home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / net / readline-2.0mg.tar.gz / readline-2.0mg.tar / readline-2.0mg / readline.c < prev    next >
C/C++ Source or Header  |  1994-11-20  |  80KB  |  3,532 lines

  1. /* readline.c -- a general facility for reading lines of input
  2.    with emacs style editing and completion. */
  3.  
  4. /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
  5.  
  6.    This file is part of the GNU Readline Library, a library for
  7.    reading lines of text with interactive input and history editing.
  8.  
  9.    The GNU Readline Library is free software; you can redistribute it
  10.    and/or modify it under the terms of the GNU General Public License
  11.    as published by the Free Software Foundation; either version 1, or
  12.    (at your option) any later version.
  13.  
  14.    The GNU Readline Library is distributed in the hope that it will be
  15.    useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  16.    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.    GNU General Public License for more details.
  18.  
  19.    The GNU General Public License is often shipped with GNU software, and
  20.    is generally kept in a file called COPYING or LICENSE.  If you do not
  21.    have a copy of the license, write to the Free Software Foundation,
  22.    675 Mass Ave, Cambridge, MA 02139, USA. */
  23. #define READLINE_LIBRARY
  24.  
  25. #if defined (HAVE_CONFIG_H)
  26. #  include "config.h"
  27. #endif
  28.  
  29. #include <stdio.h>
  30. #include <sys/types.h>
  31. #include <fcntl.h>
  32. #if !defined (NO_SYS_FILE)
  33. #  include <sys/file.h>
  34. #endif /* !NO_SYS_FILE */
  35. #include <signal.h>
  36.  
  37. #if defined (HAVE_UNISTD_H)
  38. #  include <unistd.h>
  39. #endif /* HAVE_UNISTD_H */
  40.  
  41. #if defined (HAVE_STDLIB_H)
  42. #  include <stdlib.h>
  43. #else
  44. #  include "ansi_stdlib.h"
  45. #endif /* HAVE_STDLIB_H */
  46.  
  47. #include <errno.h>
  48. /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
  49. #if !defined (errno)
  50. extern int errno;
  51. #endif /* !errno */
  52.  
  53. #include <setjmp.h>
  54.  
  55. #include "posixstat.h"
  56.  
  57. /* System-specific feature definitions and include files. */
  58. #include "rldefs.h"
  59.  
  60. #if defined (GWINSZ_IN_SYS_IOCTL) || (defined (VSTATUS) && !defined (SunOS4))
  61. #  include <sys/ioctl.h>
  62. #endif /* GWINSZ_IN_SYS_IOCTL || VSTATUS */
  63.  
  64. /* Some standard library routines. */
  65. #include "readline.h"
  66. #include "history.h"
  67.  
  68. /* NOTE: Functions and variables prefixed with `_rl_' are
  69.    pseudo-global: they are global so they can be shared
  70.    between files in the readline library, but are not intended
  71.    to be visible to readline callers. */
  72.  
  73. /* Functions imported from other files in the library. */
  74. extern char *tgetstr ();
  75. extern void rl_prep_terminal (), rl_deprep_terminal ();
  76.  
  77. extern void _rl_bind_if_unbound ();
  78.  
  79. /* External redisplay functions and variables from display.c */
  80. extern void _rl_move_vert ();
  81. extern void _rl_update_final ();
  82.  
  83. extern void _rl_erase_at_end_of_line ();
  84. extern void _rl_move_cursor_relative ();
  85.  
  86. extern int _rl_vis_botlin;
  87. extern int _rl_last_c_pos;
  88. extern int _rl_horizontal_scroll_mode;
  89. extern int rl_display_fixed;
  90. extern char *rl_display_prompt;
  91.  
  92. /* Variables imported from complete.c. */
  93. extern char *rl_completer_word_break_characters;
  94. extern char *rl_basic_word_break_characters;
  95. extern int rl_completion_query_items;
  96. extern int rl_complete_with_tilde_expansion;
  97.  
  98. #if defined (VI_MODE)
  99. extern void _rl_vi_set_last ();
  100. extern void _rl_vi_reset_last ();
  101. extern void _rl_vi_done_inserting ();
  102. #endif /* VI_MODE */
  103.  
  104. /* Forward declarations used in this file. */
  105. void _rl_free_history_entry ();
  106.  
  107. int _rl_dispatch ();
  108. void _rl_set_screen_size ();
  109. int _rl_output_character_function ();
  110.  
  111. static char *readline_internal ();
  112. static void readline_initialize_everything ();
  113. static int init_terminal_io ();
  114. static void start_using_history ();
  115. static void bind_arrow_keys ();
  116.  
  117. #if !defined (__GO32__)
  118. static void readline_default_bindings ();
  119. #endif /* !__GO32__ */
  120.  
  121. #if defined (__GO32__)
  122. #  include <sys/pc.h>
  123. #  undef HANDLE_SIGNALS
  124. #endif /* __GO32__ */
  125.  
  126. #if defined (STATIC_MALLOC)
  127. static char *xmalloc (), *xrealloc ();
  128. #else
  129. extern char *xmalloc (), *xrealloc ();
  130. #endif /* STATIC_MALLOC */
  131.  
  132.  
  133. /* **************************************************************** */
  134. /*                                    */
  135. /*            Line editing input utility            */
  136. /*                                    */
  137. /* **************************************************************** */
  138.  
  139. static char *LibraryVersion = "2.0";
  140.  
  141. /* A pointer to the keymap that is currently in use.
  142.    By default, it is the standard emacs keymap. */
  143. Keymap _rl_keymap = emacs_standard_keymap;
  144.  
  145. /* The current style of editing. */
  146. int rl_editing_mode = emacs_mode;
  147.  
  148. /* Non-zero if the previous command was a kill command. */
  149. static int last_command_was_kill = 0;
  150.  
  151. /* The current value of the numeric argument specified by the user. */
  152. int rl_numeric_arg = 1;
  153.  
  154. /* Non-zero if an argument was typed. */
  155. int rl_explicit_arg = 0;
  156.  
  157. /* Temporary value used while generating the argument. */
  158. int rl_arg_sign = 1;
  159.  
  160. /* Non-zero means we have been called at least once before. */
  161. static int rl_initialized = 0;
  162.  
  163. /* If non-zero, this program is running in an EMACS buffer. */
  164. static int running_in_emacs = 0;
  165.  
  166. /* The current offset in the current input line. */
  167. int rl_point;
  168.  
  169. /* Mark in the current input line. */
  170. int rl_mark;
  171.  
  172. /* Length of the current input line. */
  173. int rl_end;
  174.  
  175. /* Make this non-zero to return the current input_line. */
  176. int rl_done;
  177.  
  178. /* The last function executed by readline. */
  179. Function *rl_last_func = (Function *)NULL;
  180.  
  181. /* Top level environment for readline_internal (). */
  182. static jmp_buf readline_top_level;
  183.  
  184. /* The streams we interact with. */
  185. static FILE *in_stream, *out_stream;
  186.  
  187. /* The names of the streams that we do input and output to. */
  188. FILE *rl_instream = (FILE *)NULL;
  189. FILE *rl_outstream = (FILE *)NULL;
  190.  
  191. /* Non-zero means echo characters as they are read. */
  192. int readline_echoing_p = 1;
  193.  
  194. /* Current prompt. */
  195. char *rl_prompt;
  196. int rl_visible_prompt_length = 0;
  197.  
  198. /* The number of characters read in order to type this complete command. */
  199. int rl_key_sequence_length = 0;
  200.  
  201. /* If non-zero, then this is the address of a function to call just
  202.    before readline_internal () prints the first prompt. */
  203. Function *rl_startup_hook = (Function *)NULL;
  204.  
  205. /* What we use internally.  You should always refer to RL_LINE_BUFFER. */
  206. static char *the_line;
  207.  
  208. /* The character that can generate an EOF.  Really read from
  209.    the terminal driver... just defaulted here. */
  210. int _rl_eof_char = CTRL ('D');
  211.  
  212. /* Non-zero makes this the next keystroke to read. */
  213. int rl_pending_input = 0;
  214.  
  215. /* Pointer to a useful terminal name. */
  216. char *rl_terminal_name = (char *)NULL;
  217.  
  218. /* Non-zero means to always use horizontal scrolling in line display. */
  219. int _rl_horizontal_scroll_mode = 0;
  220.  
  221. /* Non-zero means to display an asterisk at the starts of history lines
  222.    which have been modified. */
  223. int _rl_mark_modified_lines = 0;  
  224.  
  225. /* The style of `bell' notification preferred.  This can be set to NO_BELL,
  226.    AUDIBLE_BELL, or VISIBLE_BELL. */
  227. int _rl_bell_preference = AUDIBLE_BELL;
  228.      
  229. /* Line buffer and maintenence. */
  230. char *rl_line_buffer = (char *)NULL;
  231. int rl_line_buffer_len = 0;
  232. #define DEFAULT_BUFFER_SIZE 256
  233.  
  234. /* Forward declarations used by the display and termcap code. */
  235. int term_xn;
  236. int screenwidth, screenheight, screenchars;
  237.  
  238.  
  239. /* **************************************************************** */
  240. /*                                    */
  241. /*            `Forward' declarations              */
  242. /*                                    */
  243. /* **************************************************************** */
  244.  
  245. /* Non-zero means do not parse any lines other than comments and
  246.    parser directives. */
  247. unsigned char _rl_parsing_conditionalized_out = 0;
  248.  
  249. /* Non-zero means to save keys that we dispatch on in a kbd macro. */
  250. static int defining_kbd_macro = 0;
  251.  
  252. /* Non-zero means to convert characters with the meta bit set to
  253.    escape-prefixed characters so we can indirect through
  254.    emacs_meta_keymap or vi_escape_keymap. */
  255. int _rl_convert_meta_chars_to_ascii = 1;
  256.  
  257. /* Non-zero means to output characters with the meta bit set directly
  258.    rather than as a meta-prefixed escape sequence. */
  259. int _rl_output_meta_chars = 0;
  260.  
  261. /* Non-zero tells rl_delete_text and rl_insert_text to not add to
  262.    the undo list. */
  263. static int doing_an_undo = 0;
  264.  
  265. /* **************************************************************** */
  266. /*                                    */
  267. /*            Top Level Functions                */
  268. /*                                    */
  269. /* **************************************************************** */
  270.  
  271. /* Non-zero means treat 0200 bit in terminal input as Meta bit. */
  272. int _rl_meta_flag = 0;    /* Forward declaration */
  273.  
  274. /* Read a line of input.  Prompt with PROMPT.  A NULL PROMPT means
  275.    none.  A return value of NULL means that EOF was encountered. */
  276. char *
  277. readline (prompt)
  278.      char *prompt;
  279. {
  280.   char *value;
  281.  
  282.   rl_prompt = prompt;
  283.  
  284.   /* If we are at EOF return a NULL string. */
  285.   if (rl_pending_input == EOF)
  286.     {
  287.       rl_pending_input = 0;
  288.       return ((char *)NULL);
  289.     }
  290.  
  291.   rl_visible_prompt_length = rl_expand_prompt (rl_prompt);
  292.  
  293.   rl_initialize ();
  294.   rl_prep_terminal (_rl_meta_flag);
  295.  
  296. #if defined (HANDLE_SIGNALS)
  297.   rl_set_signals ();
  298. #endif
  299.  
  300.   value = readline_internal ();
  301.   rl_deprep_terminal ();
  302.  
  303. #if defined (HANDLE_SIGNALS)
  304.   rl_clear_signals ();
  305. #endif
  306.  
  307.   return (value);
  308. }
  309.  
  310. /* Read a line of input from the global rl_instream, doing output on
  311.    the global rl_outstream.
  312.    If rl_prompt is non-null, then that is our prompt. */
  313. static char *
  314. readline_internal ()
  315. {
  316.   int lastc, c, eof_found;
  317.  
  318.   in_stream  = rl_instream;
  319.   out_stream = rl_outstream;
  320.  
  321.   lastc = -1;
  322.   eof_found = 0;
  323.  
  324.   if (rl_startup_hook)
  325.     (*rl_startup_hook) ();
  326.  
  327.   if (!readline_echoing_p)
  328.     {
  329.       if (rl_prompt)
  330.     {
  331.       fprintf (out_stream, "%s", rl_prompt);
  332.       fflush (out_stream);
  333.     }
  334.     }
  335.   else
  336.     {
  337.       rl_on_new_line ();
  338.       rl_redisplay ();
  339. #if defined (VI_MODE)
  340.       if (rl_editing_mode == vi_mode)
  341.     rl_vi_insertion_mode ();
  342. #endif /* VI_MODE */
  343.     }
  344.  
  345.   while (!rl_done)
  346.     {
  347.       int lk = last_command_was_kill;
  348.       int code;
  349.  
  350.       code = setjmp (readline_top_level);
  351.  
  352.       if (code)
  353.     rl_redisplay ();
  354.  
  355.       if (!rl_pending_input)
  356.     {
  357.       /* Then initialize the argument and number of keys read. */
  358.       rl_init_argument ();
  359.       rl_key_sequence_length = 0;
  360.     }
  361.  
  362.       c = rl_read_key ();
  363.  
  364.       /* EOF typed to a non-blank line is a <NL>. */
  365.       if (c == EOF && rl_end)
  366.     c = NEWLINE;
  367.  
  368.       /* The character _rl_eof_char typed to blank line, and not as the
  369.      previous character is interpreted as EOF. */
  370.       if (((c == _rl_eof_char && lastc != c) || c == EOF) && !rl_end)
  371.     {
  372.       eof_found = 1;
  373.       break;
  374.     }
  375.  
  376.       lastc = c;
  377.       _rl_dispatch (c, _rl_keymap);
  378.  
  379.       /* If there was no change in last_command_was_kill, then no kill
  380.      has taken place.  Note that if input is pending we are reading
  381.      a prefix command, so nothing has changed yet. */
  382.       if (!rl_pending_input)
  383.     {
  384.       if (lk == last_command_was_kill)
  385.         last_command_was_kill = 0;
  386.     }
  387.  
  388. #if defined (VI_MODE)
  389.       /* In vi mode, when you exit insert mode, the cursor moves back
  390.      over the previous character.  We explicitly check for that here. */
  391.       if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap)
  392.     rl_vi_check ();
  393. #endif /* VI_MODE */
  394.  
  395.       if (!rl_done)
  396.     rl_redisplay ();
  397.     }
  398.  
  399.   /* Restore the original of this history line, iff the line that we
  400.      are editing was originally in the history, AND the line has changed. */
  401.   {
  402.     HIST_ENTRY *entry = current_history ();
  403.  
  404.     if (entry && rl_undo_list)
  405.       {
  406.     char *temp = savestring (the_line);
  407.     rl_revert_line ();
  408.     entry = replace_history_entry (where_history (), the_line,
  409.                        (HIST_ENTRY *)NULL);
  410.     _rl_free_history_entry (entry);
  411.  
  412.     strcpy (the_line, temp);
  413.     free (temp);
  414.       }
  415.   }
  416.  
  417.   /* At any rate, it is highly likely that this line has an undo list.  Get
  418.      rid of it now. */
  419.   if (rl_undo_list)
  420.     free_undo_list ();
  421.  
  422.   if (eof_found)
  423.     return (char *)NULL;
  424.   else
  425.     return (savestring (the_line));
  426. }
  427.  
  428. /* **************************************************************** */
  429. /*                                    */
  430. /*            Character Input Buffering               */
  431. /*                                    */
  432. /* **************************************************************** */
  433.  
  434. static int pop_index = 0, push_index = 0, ibuffer_len = 511;
  435. static unsigned char ibuffer[512];
  436.  
  437. /* Non-null means it is a pointer to a function to run while waiting for
  438.    character input. */
  439. Function *rl_event_hook = (Function *)NULL;
  440.  
  441. #define any_typein (push_index != pop_index)
  442.  
  443. /* Add KEY to the buffer of characters to be read. */
  444. rl_stuff_char (key)
  445.      int key;
  446. {
  447.   if (key == EOF)
  448.     {
  449.       key = NEWLINE;
  450.       rl_pending_input = EOF;
  451.     }
  452.   ibuffer[push_index++] = key;
  453.   if (push_index >= ibuffer_len)
  454.     push_index = 0;
  455.   return push_index;
  456. }
  457.  
  458. /* Return the amount of space available in the
  459.    buffer for stuffing characters. */
  460. int
  461. ibuffer_space ()
  462. {
  463.   if (pop_index > push_index)
  464.     return (pop_index - push_index);
  465.   else
  466.     return (ibuffer_len - (push_index - pop_index));
  467. }
  468.  
  469. /* Get a key from the buffer of characters to be read.
  470.    Return the key in KEY.
  471.    Result is KEY if there was a key, or 0 if there wasn't. */
  472. int
  473. rl_get_char (key)
  474.      int *key;
  475. {
  476.   if (push_index == pop_index)
  477.     return (0);
  478.  
  479.   *key = ibuffer[pop_index++];
  480.  
  481.   if (pop_index >= ibuffer_len)
  482.     pop_index = 0;
  483.  
  484.   return (1);
  485. }
  486.  
  487. /* Stuff KEY into the *front* of the input buffer.
  488.    Returns non-zero if successful, zero if there is
  489.    no space left in the buffer. */
  490. int
  491. rl_unget_char (key)
  492.      int key;
  493. {
  494.   if (ibuffer_space ())
  495.     {
  496.       pop_index--;
  497.       if (pop_index < 0)
  498.     pop_index = ibuffer_len - 1;
  499.       ibuffer[pop_index] = key;
  500.       return (1);
  501.     }
  502.   return (0);
  503. }
  504.  
  505. /* If a character is available to be read, then read it
  506.    and stuff it into IBUFFER.  Otherwise, just return. */
  507. void
  508. rl_gather_tyi ()
  509. {
  510. #if defined (__GO32__)
  511.   char input;
  512.  
  513.   if (isatty (0))
  514.     {
  515.       int i = rl_getc ();
  516.  
  517.       if (i != EOF)
  518.     rl_stuff_char (i);
  519.     }
  520.   else if (kbhit () && ibuffer_space ())
  521.     rl_stuff_char (getkey ());
  522. #else /* !__GO32__ */
  523.  
  524.   int tty = fileno (in_stream);
  525.   register int tem, result = -1;
  526.   int chars_avail;
  527.   char input;
  528.  
  529. #if defined (FIONREAD)
  530.   result = ioctl (tty, FIONREAD, &chars_avail);
  531. #endif
  532.  
  533. #if defined (O_NDELAY)
  534.   if (result == -1)
  535.     {
  536.       int flags;
  537.  
  538.       flags = fcntl (tty, F_GETFL, 0);
  539.  
  540.       fcntl (tty, F_SETFL, (flags | O_NDELAY));
  541.       chars_avail = read (tty, &input, 1);
  542.  
  543.       fcntl (tty, F_SETFL, flags);
  544.       if (chars_avail == -1 && errno == EAGAIN)
  545.     return;
  546.     }
  547. #endif /* O_NDELAY */
  548.  
  549.   /* If there's nothing available, don't waste time trying to read
  550.      something. */
  551.   if (chars_avail == 0)
  552.     return;
  553.  
  554.   tem = ibuffer_space ();
  555.  
  556.   if (chars_avail > tem)
  557.     chars_avail = tem;
  558.  
  559.   /* One cannot read all of the available input.  I can only read a single
  560.      character at a time, or else programs which require input can be
  561.      thwarted.  If the buffer is larger than one character, I lose.
  562.      Damn! */
  563.   if (tem < ibuffer_len)
  564.     chars_avail = 0;
  565.  
  566.   if (result != -1)
  567.     {
  568.       while (chars_avail--)
  569.     rl_stuff_char (rl_getc (in_stream));
  570.     }
  571.   else
  572.     {
  573.       if (chars_avail)
  574.     rl_stuff_char (input);
  575.     }
  576. #endif /* !__GO32__ */
  577. }
  578.  
  579. static int next_macro_key ();
  580. /* Read a key, including pending input. */
  581. int
  582. rl_read_key ()
  583. {
  584.   int c;
  585.  
  586.   rl_key_sequence_length++;
  587.  
  588.   if (rl_pending_input)
  589.     {
  590.       c = rl_pending_input;
  591.       rl_pending_input = 0;
  592.     }
  593.   else
  594.     {
  595.       /* If input is coming from a macro, then use that. */
  596.       if (c = next_macro_key ())
  597.     return (c);
  598.  
  599.       /* If the user has an event function, then call it periodically. */
  600.       if (rl_event_hook)
  601.     {
  602.       while (rl_event_hook && !rl_get_char (&c))
  603.         {
  604.           (*rl_event_hook) ();
  605.           rl_gather_tyi ();
  606.         }
  607.     }
  608.       else
  609.     {
  610.       if (!rl_get_char (&c))
  611.         c = rl_getc (in_stream);
  612.     }
  613.     }
  614.  
  615.   return (c);
  616. }
  617.  
  618. /* Found later in this file. */
  619. static void add_macro_char (), with_macro_input ();
  620.  
  621. /* Do the command associated with KEY in MAP.
  622.    If the associated command is really a keymap, then read
  623.    another key, and dispatch into that map. */
  624. int
  625. _rl_dispatch (key, map)
  626.      register int key;
  627.      Keymap map;
  628. {
  629.   int r = 0;
  630.  
  631.   if (defining_kbd_macro)
  632.     add_macro_char (key);
  633.  
  634.   if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
  635.     {
  636.       if (map[ESC].type == ISKMAP)
  637.     {
  638.       map = FUNCTION_TO_KEYMAP (map, ESC);
  639.       key = UNMETA (key);
  640.       rl_key_sequence_length += 2;
  641.       return (_rl_dispatch (key, map));
  642.     }
  643.       else
  644.     ding ();
  645.       return 0;
  646.     }
  647.  
  648.   switch (map[key].type)
  649.     {
  650.     case ISFUNC:
  651.       {
  652.     Function *func = map[key].function;
  653.  
  654.     if (func != (Function *)NULL)
  655.       {
  656.         /* Special case rl_do_lowercase_version (). */
  657.         if (func == rl_do_lowercase_version)
  658.           return (_rl_dispatch (to_lower (key), map));
  659.  
  660.         r = (*map[key].function)(rl_numeric_arg * rl_arg_sign, key);
  661.  
  662.         /* If we have input pending, then the last command was a prefix
  663.            command.  Don't change the state of rl_last_func.  Otherwise,
  664.            remember the last command executed in this variable. */
  665.         if (!rl_pending_input)
  666.           rl_last_func = map[key].function;
  667.       }
  668.     else
  669.       {
  670.         rl_abort ();
  671.         return -1;
  672.       }
  673.       }
  674.       break;
  675.  
  676.     case ISKMAP:
  677.       if (map[key].function != (Function *)NULL)
  678.     {
  679.       int newkey;
  680.  
  681.       rl_key_sequence_length++;
  682.       newkey = rl_read_key ();
  683.       r = _rl_dispatch (newkey, FUNCTION_TO_KEYMAP (map, key));
  684.     }
  685.       else
  686.     {
  687.       rl_abort ();
  688.       return -1;
  689.     }
  690.       break;
  691.  
  692.     case ISMACR:
  693.       if (map[key].function != (Function *)NULL)
  694.     {
  695.       char *macro;
  696.  
  697.       macro = savestring ((char *)map[key].function);
  698.       with_macro_input (macro);
  699.       return 0;
  700.     }
  701.       break;
  702.     }
  703. #if defined (VI_MODE)
  704.   if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap &&
  705.       rl_vi_textmod_command (key))
  706.     _rl_vi_set_last (key, rl_numeric_arg, rl_arg_sign);
  707. #endif
  708.   return (r);
  709. }
  710.  
  711.  
  712. /* **************************************************************** */
  713. /*                                    */
  714. /*            Hacking Keyboard Macros             */
  715. /*                                    */
  716. /* **************************************************************** */
  717.  
  718. /* The currently executing macro string.  If this is non-zero,
  719.    then it is a malloc ()'ed string where input is coming from. */
  720. static char *executing_macro = (char *)NULL;
  721.  
  722. /* The offset in the above string to the next character to be read. */
  723. static int executing_macro_index = 0;
  724.  
  725. /* The current macro string being built.  Characters get stuffed
  726.    in here by add_macro_char (). */
  727. static char *current_macro = (char *)NULL;
  728.  
  729. /* The size of the buffer allocated to current_macro. */
  730. static int current_macro_size = 0;
  731.  
  732. /* The index at which characters are being added to current_macro. */
  733. static int current_macro_index = 0;
  734.  
  735. /* A structure used to save nested macro strings.
  736.    It is a linked list of string/index for each saved macro. */
  737. struct saved_macro {
  738.   struct saved_macro *next;
  739.   char *string;
  740.   int sindex;
  741. };
  742.  
  743. /* The list of saved macros. */
  744. struct saved_macro *macro_list = (struct saved_macro *)NULL;
  745.  
  746. /* Forward declarations of static functions.  Thank you C. */
  747. static void push_executing_macro (), pop_executing_macro ();
  748.  
  749. /* This one has to be declared earlier in the file. */
  750. /* static void add_macro_char (); */
  751.  
  752. /* Set up to read subsequent input from STRING.
  753.    STRING is free ()'ed when we are done with it. */
  754. static void
  755. with_macro_input (string)
  756.      char *string;
  757. {
  758.   push_executing_macro ();
  759.   executing_macro = string;
  760.   executing_macro_index = 0;
  761. }
  762.  
  763. /* Return the next character available from a macro, or 0 if
  764.    there are no macro characters. */
  765. static int
  766. next_macro_key ()
  767. {
  768.   if (!executing_macro)
  769.     return (0);
  770.  
  771.   if (!executing_macro[executing_macro_index])
  772.     {
  773.       pop_executing_macro ();
  774.       return (next_macro_key ());
  775.     }
  776.  
  777.   return (executing_macro[executing_macro_index++]);
  778. }
  779.  
  780. /* Save the currently executing macro on a stack of saved macros. */
  781. static void
  782. push_executing_macro ()
  783. {
  784.   struct saved_macro *saver;
  785.  
  786.   saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro));
  787.   saver->next = macro_list;
  788.   saver->sindex = executing_macro_index;
  789.   saver->string = executing_macro;
  790.  
  791.   macro_list = saver;
  792. }
  793.  
  794. /* Discard the current macro, replacing it with the one
  795.    on the top of the stack of saved macros. */
  796. static void
  797. pop_executing_macro ()
  798. {
  799.   if (executing_macro)
  800.     free (executing_macro);
  801.  
  802.   executing_macro = (char *)NULL;
  803.   executing_macro_index = 0;
  804.  
  805.   if (macro_list)
  806.     {
  807.       struct saved_macro *disposer = macro_list;
  808.       executing_macro = macro_list->string;
  809.       executing_macro_index = macro_list->sindex;
  810.       macro_list = macro_list->next;
  811.       free (disposer);
  812.     }
  813. }
  814.  
  815. /* Add a character to the macro being built. */
  816. static void
  817. add_macro_char (c)
  818.      int c;
  819. {
  820.   if (current_macro_index + 1 >= current_macro_size)
  821.     {
  822.       if (!current_macro)
  823.     current_macro = xmalloc (current_macro_size = 25);
  824.       else
  825.     current_macro = xrealloc (current_macro, current_macro_size += 25);
  826.     }
  827.  
  828.   current_macro[current_macro_index++] = c;
  829.   current_macro[current_macro_index] = '\0';
  830. }
  831.  
  832. /* Begin defining a keyboard macro.
  833.    Keystrokes are recorded as they are executed.
  834.    End the definition with rl_end_kbd_macro ().
  835.    If a numeric argument was explicitly typed, then append this
  836.    definition to the end of the existing macro, and start by
  837.    re-executing the existing macro. */
  838. rl_start_kbd_macro (ignore1, ignore2)
  839.      int ignore1, ignore2;
  840. {
  841.   if (defining_kbd_macro)
  842.     {
  843.       rl_abort ();
  844.       return -1;
  845.     }
  846.  
  847.   if (rl_explicit_arg)
  848.     {
  849.       if (current_macro)
  850.     with_macro_input (savestring (current_macro));
  851.     }
  852.   else
  853.     current_macro_index = 0;
  854.  
  855.   defining_kbd_macro = 1;
  856.   return 0;
  857. }
  858.  
  859. /* Stop defining a keyboard macro.
  860.    A numeric argument says to execute the macro right now,
  861.    that many times, counting the definition as the first time. */
  862. rl_end_kbd_macro (count, ignore)
  863.      int count, ignore;
  864. {
  865.   if (!defining_kbd_macro)
  866.     {
  867.       rl_abort ();
  868.       return -1;
  869.     }
  870.  
  871.   current_macro_index -= (rl_key_sequence_length - 1);
  872.   current_macro[current_macro_index] = '\0';
  873.  
  874.   defining_kbd_macro = 0;
  875.  
  876.   return (rl_call_last_kbd_macro (--count, 0));
  877. }
  878.  
  879. /* Execute the most recently defined keyboard macro.
  880.    COUNT says how many times to execute it. */
  881. rl_call_last_kbd_macro (count, ignore)
  882.      int count, ignore;
  883. {
  884.   if (!current_macro)
  885.     rl_abort ();
  886.  
  887.   if (defining_kbd_macro)
  888.     {
  889.       ding ();        /* no recursive macros */
  890.       current_macro[--current_macro_index] = '\0';    /* erase this char */
  891.       return 0;
  892.     }
  893.  
  894.   while (count--)
  895.     with_macro_input (savestring (current_macro));
  896.   return 0;
  897. }
  898.  
  899. void
  900. _rl_kill_kbd_macro ()
  901. {
  902.   if (current_macro)
  903.     {
  904.       free (current_macro);
  905.       current_macro = (char *) NULL;
  906.     }
  907.   current_macro_size = current_macro_index = 0;
  908.  
  909.   if (executing_macro)
  910.     {
  911.       free (executing_macro);
  912.       executing_macro = (char *) NULL;
  913.     }
  914.   executing_macro_index = 0;
  915.  
  916.   defining_kbd_macro = 0;
  917. }
  918.  
  919. /* **************************************************************** */
  920. /*                                    */
  921. /*            Initializations                 */
  922. /*                                    */
  923. /* **************************************************************** */
  924.  
  925. /* Initliaze readline (and terminal if not already). */
  926. rl_initialize ()
  927. {
  928.   char *t;
  929.  
  930.   /* If we have never been called before, initialize the
  931.      terminal and data structures. */
  932.   if (!rl_initialized)
  933.     {
  934.       readline_initialize_everything ();
  935.       rl_initialized++;
  936.     }
  937.  
  938.   /* Initalize the current line information. */
  939.   rl_point = rl_end = 0;
  940.   the_line = rl_line_buffer;
  941.   the_line[0] = 0;
  942.  
  943.   /* We aren't done yet.  We haven't even gotten started yet! */
  944.   rl_done = 0;
  945.  
  946.   /* Check for LC_CTYPE and use its value to decide the defaults for
  947.      8-bit character input and output. */
  948.   t = getenv ("LC_CTYPE");
  949.   if (t && (strcmp (t, "iso-8859-1") == 0 || strcmp (t, "iso_8859_1") == 0))
  950.     {
  951.       _rl_meta_flag = 1;
  952.       _rl_convert_meta_chars_to_ascii = 0;
  953.       _rl_output_meta_chars = 1;
  954.     }
  955.       
  956.   /* Tell the history routines what is going on. */
  957.   start_using_history ();
  958.  
  959.   /* Make the display buffer match the state of the line. */
  960.   rl_reset_line_state ();
  961.  
  962.   /* No such function typed yet. */
  963.   rl_last_func = (Function *)NULL;
  964.  
  965.   /* Parsing of key-bindings begins in an enabled state. */
  966.   _rl_parsing_conditionalized_out = 0;
  967.  
  968.   return 0;
  969. }
  970.  
  971. /* Initialize the entire state of the world. */
  972. static void
  973. readline_initialize_everything ()
  974. {
  975.   /* Find out if we are running in Emacs. */
  976.   running_in_emacs = getenv ("EMACS") != (char *)0;
  977.  
  978.   /* Set up input and output if they are not already set up. */
  979.   if (!rl_instream)
  980.     rl_instream = stdin;
  981.  
  982.   if (!rl_outstream)
  983.     rl_outstream = stdout;
  984.  
  985.   /* Bind in_stream and out_stream immediately.  These values may change,
  986.      but they may also be used before readline_internal () is called. */
  987.   in_stream = rl_instream;
  988.   out_stream = rl_outstream;
  989.  
  990.   /* Allocate data structures. */
  991.   if (!rl_line_buffer)
  992.     rl_line_buffer = xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
  993.  
  994.   /* Initialize the terminal interface. */
  995.   init_terminal_io ((char *)NULL);
  996.  
  997. #if !defined (__GO32__)
  998.   /* Bind tty characters to readline functions. */
  999.   readline_default_bindings ();
  1000. #endif /* !__GO32__ */
  1001.  
  1002.   /* Initialize the function names. */
  1003.   rl_initialize_funmap ();
  1004.  
  1005.   /* Read in the init file. */
  1006.   rl_read_init_file ((char *)NULL);
  1007.  
  1008.   /* XXX */
  1009.   if (_rl_horizontal_scroll_mode && term_xn)
  1010.     {
  1011.       screenwidth--;
  1012.       screenchars -= screenheight;
  1013.     }
  1014.  
  1015.   /* Override the effect of any `set keymap' assignments in the
  1016.      inputrc file. */
  1017.   rl_set_keymap_from_edit_mode ();
  1018.  
  1019.   /* Try to bind a common arrow key prefix, if not already bound. */
  1020.   bind_arrow_keys ();
  1021.  
  1022.   /* If the completion parser's default word break characters haven't
  1023.      been set yet, then do so now. */
  1024.   if (rl_completer_word_break_characters == (char *)NULL)
  1025.     rl_completer_word_break_characters = rl_basic_word_break_characters;
  1026. }
  1027.  
  1028. /* If this system allows us to look at the values of the regular
  1029.    input editing characters, then bind them to their readline
  1030.    equivalents, iff the characters are not bound to keymaps. */
  1031. static void
  1032. readline_default_bindings ()
  1033. {
  1034.   rltty_set_default_bindings (_rl_keymap);
  1035. }
  1036.  
  1037. static void
  1038. bind_arrow_keys_internal ()
  1039. {
  1040.   Function *f;
  1041.  
  1042.   f = rl_function_of_keyseq ("\033[A", _rl_keymap, (int *)NULL);
  1043.   if (!f || f == rl_do_lowercase_version)
  1044.     {
  1045.       _rl_bind_if_unbound ("\033[A", rl_get_previous_history);
  1046.       _rl_bind_if_unbound ("\033[B", rl_get_next_history);
  1047.       _rl_bind_if_unbound ("\033[C", rl_forward);
  1048.       _rl_bind_if_unbound ("\033[D", rl_backward);
  1049.     }
  1050.  
  1051.   f = rl_function_of_keyseq ("\033OA", _rl_keymap, (int *)NULL);
  1052.   if (!f || f == rl_do_lowercase_version)
  1053.     {
  1054.       _rl_bind_if_unbound ("\033OA", rl_get_previous_history);
  1055.       _rl_bind_if_unbound ("\033OB", rl_get_next_history);
  1056.       _rl_bind_if_unbound ("\033OC", rl_forward);
  1057.       _rl_bind_if_unbound ("\033OD", rl_backward);
  1058.     }
  1059. }
  1060.  
  1061. /* Try and bind the common arrow key prefix after giving termcap and
  1062.    the inputrc file a chance to bind them and create `real' keymaps
  1063.    for the arrow key prefix. */
  1064. static void
  1065. bind_arrow_keys ()
  1066. {
  1067.   Keymap xkeymap;
  1068.  
  1069.   xkeymap = _rl_keymap;
  1070.  
  1071.   _rl_keymap = emacs_standard_keymap;
  1072.   bind_arrow_keys_internal ();
  1073.  
  1074. #if defined (VI_MODE)
  1075.   _rl_keymap = vi_movement_keymap;
  1076.   bind_arrow_keys_internal ();
  1077. #endif
  1078.  
  1079.   _rl_keymap = xkeymap;
  1080. }
  1081.  
  1082.  
  1083. /* **************************************************************** */
  1084. /*                                    */
  1085. /*            Numeric Arguments                */
  1086. /*                                    */
  1087. /* **************************************************************** */
  1088.  
  1089. /* Handle C-u style numeric args, as well as M--, and M-digits. */
  1090. static int
  1091. rl_digit_loop ()
  1092. {
  1093.   int key, c;
  1094.  
  1095.   while (1)
  1096.     {
  1097.       rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
  1098.       key = c = rl_read_key ();
  1099.  
  1100.       if (_rl_keymap[c].type == ISFUNC &&
  1101.       _rl_keymap[c].function == rl_universal_argument)
  1102.     {
  1103.       rl_numeric_arg *= 4;
  1104.       continue;
  1105.     }
  1106.       c = UNMETA (c);
  1107.       if (digit_p (c))
  1108.     {
  1109.       if (rl_explicit_arg)
  1110.         rl_numeric_arg = (rl_numeric_arg * 10) + (c - '0');
  1111.       else
  1112.         rl_numeric_arg = (c - '0');
  1113.       rl_explicit_arg = 1;
  1114.     }
  1115.       else
  1116.     {
  1117.       if (c == '-' && !rl_explicit_arg)
  1118.         {
  1119.           rl_numeric_arg = 1;
  1120.           rl_arg_sign = -1;
  1121.         }
  1122.       else
  1123.         {
  1124.           rl_clear_message ();
  1125.           return (_rl_dispatch (key, _rl_keymap));
  1126.         }
  1127.     }
  1128.     }
  1129.   return 0;
  1130. }
  1131.  
  1132. /* Add the current digit to the argument in progress. */
  1133. rl_digit_argument (ignore, key)
  1134.      int ignore, key;
  1135. {
  1136.   rl_pending_input = key;
  1137.   return (rl_digit_loop ());
  1138. }
  1139.  
  1140. /* What to do when you abort reading an argument. */
  1141. rl_discard_argument ()
  1142. {
  1143.   ding ();
  1144.   rl_clear_message ();
  1145.   rl_init_argument ();
  1146.   return 0;
  1147. }
  1148.  
  1149. /* Create a default argument. */
  1150. rl_init_argument ()
  1151. {
  1152.   rl_numeric_arg = rl_arg_sign = 1;
  1153.   rl_explicit_arg = 0;
  1154.   return 0;
  1155. }
  1156.  
  1157. /* C-u, universal argument.  Multiply the current argument by 4.
  1158.    Read a key.  If the key has nothing to do with arguments, then
  1159.    dispatch on it.  If the key is the abort character then abort. */
  1160. rl_universal_argument ()
  1161. {
  1162.   rl_numeric_arg *= 4;
  1163.   return (rl_digit_loop ());
  1164. }
  1165.  
  1166. /* **************************************************************** */
  1167. /*                                    */
  1168. /*            Terminal and Termcap                */
  1169. /*                                    */
  1170. /* **************************************************************** */
  1171.  
  1172. static char *term_buffer = (char *)NULL;
  1173. static char *term_string_buffer = (char *)NULL;
  1174.  
  1175. static int tcap_initialized = 0;
  1176.  
  1177. /* Non-zero means this terminal can't really do anything. */
  1178. int dumb_term = 0;
  1179. /* On Solaris2, sys/types.h #includes sys/reg.h, which #defines PC.
  1180.    Unfortunately, PC is a global variable used by the termcap library. */
  1181. #undef PC
  1182.  
  1183. #if !defined (__linux__)
  1184. /* If this causes problems, remove the `extern'. */
  1185. extern char PC, *BC, *UP;
  1186. #endif /* __linux__ */
  1187.  
  1188. /* Some strings to control terminal actions.  These are output by tputs (). */
  1189. char *term_goto, *term_clreol, *term_cr, *term_clrpag, *term_backspace;
  1190. char *term_pc;
  1191.  
  1192. /* Non-zero if we determine that the terminal can do character insertion. */
  1193. int terminal_can_insert = 0;
  1194.  
  1195. /* How to insert characters. */
  1196. char *term_im, *term_ei, *term_ic, *term_ip, *term_IC;
  1197.  
  1198. /* How to delete characters. */
  1199. char *term_dc, *term_DC;
  1200.  
  1201. #if defined (HACK_TERMCAP_MOTION)
  1202. char *term_forward_char;
  1203. #endif  /* HACK_TERMCAP_MOTION */
  1204.  
  1205. /* How to go up a line. */
  1206. char *term_up;
  1207.  
  1208. /* A visible bell, if the terminal can be made to flash the screen. */
  1209. char *visible_bell;
  1210.  
  1211. /* Non-zero means that this terminal has a meta key. */
  1212. int term_has_meta;
  1213.  
  1214. /* The string to write to turn on the meta key, if this term has one. */
  1215. char *term_mm;
  1216.  
  1217. /* The string to write to turn off the meta key, if this term has one. */
  1218. char *term_mo;
  1219.  
  1220. /* The key sequences output by the arrow keys, if this terminal has any. */
  1221. char *term_ku, *term_kd, *term_kr, *term_kl;
  1222.  
  1223. /* How to initialize and reset the arrow keys, if this terminal has any. */
  1224. char *term_ks, *term_ke;
  1225.  
  1226. /* Re-initialize the terminal considering that the TERM/TERMCAP variable
  1227.    has changed. */
  1228. rl_reset_terminal (terminal_name)
  1229.      char *terminal_name;
  1230. {
  1231.   init_terminal_io (terminal_name);
  1232.   return 0;
  1233. }
  1234.  
  1235. /* Set readline's idea of the screen size.  TTY is a file descriptor open
  1236.    to the terminal.  If IGNORE_ENV is true, we do not pay attention to the
  1237.    values of $LINES and $COLUMNS.  The tests for TERM_STRING_BUFFER being
  1238.    non-null serve to check whether or not we have initialized termcap. */
  1239. void
  1240. _rl_set_screen_size (tty, ignore_env)
  1241.      int tty, ignore_env;
  1242. {
  1243. #if defined (TIOCGWINSZ)
  1244.   struct winsize window_size;
  1245. #endif /* TIOCGWINSZ */
  1246.  
  1247. #if defined (TIOCGWINSZ)
  1248.   if (ioctl (tty, TIOCGWINSZ, &window_size) == 0)
  1249.     {
  1250.       screenwidth = (int) window_size.ws_col;
  1251.       screenheight = (int) window_size.ws_row;
  1252.     }
  1253. #endif /* TIOCGWINSZ */
  1254.  
  1255.   /* Environment variable COLUMNS overrides setting of "co" if IGNORE_ENV
  1256.      is unset. */
  1257.   if (screenwidth <= 0)
  1258.     {
  1259.       char *sw;
  1260.  
  1261.       if (!ignore_env && (sw = getenv ("COLUMNS")))
  1262.     screenwidth = atoi (sw);
  1263.  
  1264.       if (screenwidth <= 0 && term_string_buffer)
  1265.     screenwidth = tgetnum ("co");
  1266.     }
  1267.  
  1268.   /* Environment variable LINES overrides setting of "li" if IGNORE_ENV
  1269.      is unset. */
  1270.   if (screenheight <= 0)
  1271.     {
  1272.       char *sh;
  1273.  
  1274.       if (!ignore_env && (sh = getenv ("LINES")))
  1275.     screenheight = atoi (sh);
  1276.  
  1277.       if (screenheight <= 0 && term_string_buffer)
  1278.     screenheight = tgetnum ("li");
  1279.     }
  1280.  
  1281.   /* If all else fails, default to 80x24 terminal. */
  1282.   if (screenwidth <= 1)
  1283.     screenwidth = 80;
  1284.  
  1285.   if (screenheight <= 0)
  1286.     screenheight = 24;
  1287.  
  1288. #if defined (SHELL)
  1289.   /* If we're being compiled as part of bash, set the environment
  1290.      variables $LINES and $COLUMNS to new values. */
  1291.   set_lines_and_columns (screenheight, screenwidth);
  1292. #endif
  1293.  
  1294.   if (!term_xn)
  1295.     screenwidth--;
  1296.  
  1297.   screenchars = screenwidth * screenheight;
  1298. }
  1299.  
  1300. struct _tc_string {
  1301.      char *tc_var;
  1302.      char **tc_value;
  1303. };
  1304.  
  1305. /* This should be kept sorted, just in case we decide to change the
  1306.    search algorithm to something smarter. */
  1307. static struct _tc_string tc_strings[] =
  1308. {
  1309.   "DC", &term_DC,
  1310.   "IC", &term_IC,
  1311.   "ce", &term_clreol,
  1312.   "cl", &term_clrpag,
  1313.   "cr", &term_cr,
  1314.   "dc", &term_dc,
  1315.   "ei", &term_ei,
  1316.   "ic", &term_ic,
  1317.   "im", &term_im,
  1318.   "kd", &term_kd,
  1319.   "kl", &term_kl,
  1320.   "kr", &term_kr,
  1321.   "ku", &term_ku,
  1322.   "ks", &term_ks,
  1323.   "ke", &term_ke,
  1324.   "le", &term_backspace,
  1325.   "mm", &term_mm,
  1326.   "mo", &term_mo,
  1327. #if defined (HACK_TERMCAP_MOTION)
  1328.   "nd", &term_forward_char,
  1329. #endif
  1330.   "pc", &term_pc,
  1331.   "up", &term_up,
  1332.   "vb", &visible_bell,
  1333. };
  1334.  
  1335. #define NUM_TC_STRINGS (sizeof (tc_strings) / sizeof (struct _tc_string))
  1336.  
  1337. /* Read the desired terminal capability strings into BP.  The capabilities
  1338.    are described in the TC_STRINGS table. */
  1339. static void
  1340. get_term_capabilities (bp)
  1341.      char **bp;
  1342. {
  1343.   register int i;
  1344.  
  1345.   for (i = 0; i < NUM_TC_STRINGS; i++)
  1346.     *(tc_strings[i].tc_value) = tgetstr (tc_strings[i].tc_var, bp);
  1347.   tcap_initialized = 1;
  1348. }
  1349.  
  1350. static int
  1351. init_terminal_io (terminal_name)
  1352.      char *terminal_name;
  1353. {
  1354. #if defined (__GO32__)
  1355.   screenwidth = ScreenCols ();
  1356.   screenheight = ScreenRows ();
  1357.   screenchars = screenwidth * screenheight;
  1358.   term_cr = "\r";
  1359.   term_im = term_ei = term_ic = term_IC = (char *)NULL;
  1360.   term_up = term_dc = term_DC = visible_bell = (char *)NULL;
  1361.  
  1362.   /* Does the __GO32__ have a meta key?  I don't know. */
  1363.   term_has_meta = 0;
  1364.   term_mm = term_mo = (char *)NULL;
  1365.  
  1366.   /* It probably has arrow keys, but I don't know what they are. */
  1367.   term_ku = term_kd = term_kr = term_kl = (char *)NULL;
  1368.  
  1369. #if defined (HACK_TERMCAP_MOTION)
  1370.   term_forward_char = (char *)NULL;
  1371. #endif /* HACK_TERMCAP_MOTION */
  1372.   terminal_can_insert = term_xn = 0;
  1373.   return;
  1374. #else /* !__GO32__ */
  1375.  
  1376.   char *term, *buffer;
  1377.   int tty;
  1378.   Keymap xkeymap;
  1379.  
  1380.   term = terminal_name ? terminal_name : getenv ("TERM");
  1381.  
  1382.   if (!term_string_buffer)
  1383.     term_string_buffer = xmalloc (2048);
  1384.  
  1385.   if (!term_buffer)
  1386.     term_buffer = xmalloc (2048);
  1387.  
  1388.   buffer = term_string_buffer;
  1389.  
  1390.   term_clrpag = term_cr = term_clreol = (char *)NULL;
  1391.  
  1392.   if (!term)
  1393.     term = "dumb";
  1394.  
  1395.   if (tgetent (term_buffer, term) <= 0)
  1396.     {
  1397.       dumb_term = 1;
  1398.       screenwidth = 79;
  1399.       screenheight = 24;
  1400.       screenchars = 79 * 24;
  1401.       term_cr = "\r";
  1402.       term_im = term_ei = term_ic = term_IC = (char *)NULL;
  1403.       term_up = term_dc = term_DC = visible_bell = (char *)NULL;
  1404.       term_ku = term_kd = term_kl = term_kr = (char *)NULL;
  1405. #if defined (HACK_TERMCAP_MOTION)
  1406.       term_forward_char = (char *)NULL;
  1407. #endif
  1408.       terminal_can_insert = 0;
  1409.       return 0;
  1410.     }
  1411.  
  1412.   get_term_capabilities (&buffer);
  1413.  
  1414.   /* Set up the variables that the termcap library expects the application
  1415.      to provide. */
  1416.   PC = term_pc ? *term_pc : 0;
  1417.   BC = term_backspace;
  1418.   UP = term_up;
  1419.  
  1420.   if (!term_cr)
  1421.     term_cr =  "\r";
  1422.  
  1423.   if (rl_instream)
  1424.     tty = fileno (rl_instream);
  1425.   else
  1426.     tty = 0;
  1427.  
  1428.   screenwidth = screenheight = 0;
  1429.  
  1430.   term_xn = tgetflag ("am") && tgetflag ("xn");
  1431.  
  1432.   _rl_set_screen_size (tty, 0);
  1433.  
  1434.   /* "An application program can assume that the terminal can do
  1435.       character insertion if *any one of* the capabilities `IC',
  1436.       `im', `ic' or `ip' is provided."  But we can't do anything if
  1437.       only `ip' is provided, so... */
  1438.   terminal_can_insert = (term_IC || term_im || term_ic);
  1439.  
  1440.   /* Check to see if this terminal has a meta key and clear the capability
  1441.      variables if there is none. */
  1442.   term_has_meta = (tgetflag ("km") || tgetflag ("MT"));
  1443.   if (!term_has_meta)
  1444.     {
  1445.       term_mm = (char *)NULL;
  1446.       term_mo = (char *)NULL;
  1447.     }
  1448.  
  1449.   /* Attempt to find and bind the arrow keys.  Do not override already
  1450.      bound keys in an overzealous attempt, however. */
  1451.   xkeymap = _rl_keymap;
  1452.  
  1453.   _rl_keymap = emacs_standard_keymap;
  1454.   _rl_bind_if_unbound (term_ku, rl_get_previous_history);
  1455.   _rl_bind_if_unbound (term_kd, rl_get_next_history);
  1456.   _rl_bind_if_unbound (term_kr, rl_forward);
  1457.   _rl_bind_if_unbound (term_kl, rl_backward);
  1458.  
  1459. #if defined (VI_MODE)
  1460.   _rl_keymap = vi_movement_keymap;
  1461.   _rl_bind_if_unbound (term_ku, rl_get_previous_history);
  1462.   _rl_bind_if_unbound (term_kd, rl_get_next_history);
  1463.   _rl_bind_if_unbound (term_kr, rl_forward);
  1464.   _rl_bind_if_unbound (term_kl, rl_backward);
  1465. #endif /* VI_MODE */
  1466.  
  1467.   _rl_keymap = xkeymap;
  1468.  
  1469. #endif /* !__GO32__ */
  1470.   return 0;
  1471. }
  1472.  
  1473. char *
  1474. rl_get_termcap (cap)
  1475.      char *cap;
  1476. {
  1477.   register int i;
  1478.  
  1479.   if (tcap_initialized == 0)
  1480.     return ((char *)NULL);
  1481.   for (i = 0; i < NUM_TC_STRINGS; i++)
  1482.     {
  1483.       if (tc_strings[i].tc_var[0] == cap[0] && strcmp (tc_strings[i].tc_var, cap) == 0)
  1484.         return *(tc_strings[i].tc_value);
  1485.     }
  1486.   return ((char *)NULL);
  1487. }
  1488.  
  1489. /* A function for the use of tputs () */
  1490. int
  1491. _rl_output_character_function (c)
  1492.      int c;
  1493. {
  1494.   return putc (c, out_stream);
  1495. }
  1496.  
  1497. /* Write COUNT characters from STRING to the output stream. */
  1498. void
  1499. _rl_output_some_chars (string, count)
  1500.      char *string;
  1501.      int count;
  1502. {
  1503.   fwrite (string, 1, count, out_stream);
  1504. }
  1505.  
  1506. /* Move the cursor back. */
  1507. rl_backspace (count)
  1508.      int count;
  1509. {
  1510.   register int i;
  1511.  
  1512. #if !defined (__GO32__)
  1513.   if (term_backspace)
  1514.     for (i = 0; i < count; i++)
  1515.       tputs (term_backspace, 1, _rl_output_character_function);
  1516.   else
  1517. #endif /* !__GO32__ */
  1518.     for (i = 0; i < count; i++)
  1519.       putc ('\b', out_stream);
  1520.   return 0;
  1521. }
  1522.  
  1523. /* Move to the start of the next line. */
  1524. crlf ()
  1525. {
  1526. #if defined (NEW_TTY_DRIVER)
  1527.   tputs (term_cr, 1, _rl_output_character_function);
  1528. #endif /* NEW_TTY_DRIVER */
  1529.   putc ('\n', out_stream);
  1530.   return 0;
  1531. }
  1532.  
  1533. rl_tty_status (count, key)
  1534.      int count, key;
  1535. {
  1536. #if defined (TIOCSTAT)
  1537.   ioctl (1, TIOCSTAT, (char *)0);
  1538.   rl_refresh_line ();
  1539. #else
  1540.   ding ();
  1541. #endif
  1542.   return 0;
  1543. }
  1544.  
  1545.  
  1546. /* **************************************************************** */
  1547. /*                                    */
  1548. /*            Utility Functions                */
  1549. /*                                    */
  1550. /* **************************************************************** */
  1551.  
  1552. /* Return 0 if C is not a member of the class of characters that belong
  1553.    in words, or 1 if it is. */
  1554.  
  1555. int allow_pathname_alphabetic_chars = 0;
  1556. char *pathname_alphabetic_chars = "/-_=~.#$";
  1557.  
  1558. int
  1559. alphabetic (c)
  1560.      int c;
  1561. {
  1562.   if (pure_alphabetic (c) || (digit_p (c)))
  1563.     return (1);
  1564.  
  1565.   if (allow_pathname_alphabetic_chars)
  1566.     return (strchr (pathname_alphabetic_chars, c) != NULL);
  1567.   else
  1568.     return (0);
  1569. }
  1570.  
  1571. /* Ring the terminal bell. */
  1572. int
  1573. ding ()
  1574. {
  1575.   if (readline_echoing_p)
  1576.     {
  1577. #if !defined (__GO32__)
  1578.       switch (_rl_bell_preference)
  1579.         {
  1580.     case NO_BELL:
  1581.     default:
  1582.       break;
  1583.     case VISIBLE_BELL:
  1584.       if (visible_bell)
  1585.         {
  1586.           tputs (visible_bell, 1, _rl_output_character_function);
  1587.           break;
  1588.         }
  1589.       /* FALLTHROUGH */
  1590.     case AUDIBLE_BELL:
  1591.       fprintf (stderr, "\007");
  1592.       fflush (stderr);
  1593.       break;
  1594.         }
  1595. #else /* __GO32__ */
  1596.       fprintf (stderr, "\007");
  1597.       fflush (stderr);
  1598. #endif /* __GO32__ */
  1599.       return (0);
  1600.     }
  1601.   return (-1);
  1602. }
  1603.  
  1604. /* How to abort things. */
  1605. rl_abort (count, key)
  1606.      int count, key;
  1607. {
  1608.   ding ();
  1609.   rl_clear_message ();
  1610.   rl_init_argument ();
  1611.   rl_pending_input = 0;
  1612.  
  1613.   defining_kbd_macro = 0;
  1614.   while (executing_macro)
  1615.     pop_executing_macro ();
  1616.  
  1617.   rl_last_func = (Function *)NULL;
  1618.   longjmp (readline_top_level, 1);
  1619. }
  1620.  
  1621. /* Return a copy of the string between FROM and TO.
  1622.    FROM is inclusive, TO is not. */
  1623. char *
  1624. rl_copy_text (from, to)
  1625.      int from, to;
  1626. {
  1627.   register int length;
  1628.   char *copy;
  1629.  
  1630.   /* Fix it if the caller is confused. */
  1631.   if (from > to)
  1632.     {
  1633.       int t = from;
  1634.       from = to;
  1635.       to = t;
  1636.     }
  1637.  
  1638.   length = to - from;
  1639.   copy = xmalloc (1 + length);
  1640.   strncpy (copy, the_line + from, length);
  1641.   copy[length] = '\0';
  1642.   return (copy);
  1643. }
  1644.  
  1645. /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
  1646.    LEN characters. */
  1647. void
  1648. rl_extend_line_buffer (len)
  1649.      int len;
  1650. {
  1651.   while (len >= rl_line_buffer_len)
  1652.     {
  1653.       rl_line_buffer_len += DEFAULT_BUFFER_SIZE;
  1654.       rl_line_buffer = xrealloc (rl_line_buffer, rl_line_buffer_len);
  1655.     }
  1656.  
  1657.   the_line = rl_line_buffer;
  1658. }
  1659.  
  1660.  
  1661. /* **************************************************************** */
  1662. /*                                    */
  1663. /*            Insert and Delete                */
  1664. /*                                    */
  1665. /* **************************************************************** */
  1666.  
  1667. /* Insert a string of text into the line at point.  This is the only
  1668.    way that you should do insertion.  rl_insert () calls this
  1669.    function. */
  1670. rl_insert_text (string)
  1671.      char *string;
  1672. {
  1673.   register int i, l = strlen (string);
  1674.  
  1675.   if (rl_end + l >= rl_line_buffer_len)
  1676.     rl_extend_line_buffer (rl_end + l);
  1677.  
  1678.   for (i = rl_end; i >= rl_point; i--)
  1679.     the_line[i + l] = the_line[i];
  1680.   strncpy (the_line + rl_point, string, l);
  1681.  
  1682.   /* Remember how to undo this if we aren't undoing something. */
  1683.   if (!doing_an_undo)
  1684.     {
  1685.       /* If possible and desirable, concatenate the undos. */
  1686.       if ((l == 1) &&
  1687.       rl_undo_list &&
  1688.       (rl_undo_list->what == UNDO_INSERT) &&
  1689.       (rl_undo_list->end == rl_point) &&
  1690.       (rl_undo_list->end - rl_undo_list->start < 20))
  1691.     rl_undo_list->end++;
  1692.       else
  1693.     rl_add_undo (UNDO_INSERT, rl_point, rl_point + l, (char *)NULL);
  1694.     }
  1695.   rl_point += l;
  1696.   rl_end += l;
  1697.   the_line[rl_end] = '\0';
  1698.   return l;
  1699. }
  1700.  
  1701. /* Delete the string between FROM and TO.  FROM is
  1702.    inclusive, TO is not. */
  1703. rl_delete_text (from, to)
  1704.      int from, to;
  1705. {
  1706.   register char *text;
  1707.   register int diff, i;
  1708.  
  1709.   /* Fix it if the caller is confused. */
  1710.   if (from > to)
  1711.     {
  1712.       int t = from;
  1713.       from = to;
  1714.       to = t;
  1715.     }
  1716.   text = rl_copy_text (from, to);
  1717.  
  1718.   /* Some versions of strncpy() can't handle overlapping arguments. */
  1719.   diff = to - from;
  1720.   for (i = from; i < rl_end - diff; i++)
  1721.     the_line[i] = the_line[i + diff];
  1722.  
  1723.   /* Remember how to undo this delete. */
  1724.   if (!doing_an_undo)
  1725.     rl_add_undo (UNDO_DELETE, from, to, text);
  1726.   else
  1727.     free (text);
  1728.  
  1729.   rl_end -= diff;
  1730.   the_line[rl_end] = '\0';
  1731.   return (diff);
  1732. }
  1733.  
  1734.  
  1735. /* **************************************************************** */
  1736. /*                                    */
  1737. /*            Readline character functions            */
  1738. /*                                    */
  1739. /* **************************************************************** */
  1740.  
  1741. /* This is not a gap editor, just a stupid line input routine.  No hair
  1742.    is involved in writing any of the functions, and none should be. */
  1743.  
  1744. /* Note that:
  1745.  
  1746.    rl_end is the place in the string that we would place '\0';
  1747.    i.e., it is always safe to place '\0' there.
  1748.  
  1749.    rl_point is the place in the string where the cursor is.  Sometimes
  1750.    this is the same as rl_end.
  1751.  
  1752.    Any command that is called interactively receives two arguments.
  1753.    The first is a count: the numeric arg pased to this command.
  1754.    The second is the key which invoked this command.
  1755. */
  1756.  
  1757.  
  1758. /* **************************************************************** */
  1759. /*                                    */
  1760. /*            Movement Commands                */
  1761. /*                                    */
  1762. /* **************************************************************** */
  1763.  
  1764. /* Note that if you `optimize' the display for these functions, you cannot
  1765.    use said functions in other functions which do not do optimizing display.
  1766.    I.e., you will have to update the data base for rl_redisplay, and you
  1767.    might as well let rl_redisplay do that job. */
  1768.  
  1769. /* Move forward COUNT characters. */
  1770. rl_forward (count, key)
  1771.      int count, key;
  1772. {
  1773.   if (count < 0)
  1774.     rl_backward (-count);
  1775.   else if (count > 0)
  1776.     {
  1777.       int end = rl_point + count;
  1778. #if defined (VI_MODE)
  1779.       int lend = rl_end - (rl_editing_mode == vi_mode);
  1780. #else
  1781.       int lend = rl_end;
  1782. #endif
  1783.  
  1784.       if (end > lend)
  1785.     {
  1786.       rl_point = lend;
  1787.       ding ();
  1788.     }
  1789.       else
  1790.     rl_point = end;
  1791.     }
  1792.   return 0;
  1793. }
  1794.  
  1795. /* Move backward COUNT characters. */
  1796. rl_backward (count, key)
  1797.      int count, key;
  1798. {
  1799.   if (count < 0)
  1800.     rl_forward (-count);
  1801.   else if (count > 0)
  1802.     {
  1803.       if (rl_point < count)
  1804.     {
  1805.       rl_point = 0;
  1806.       ding ();
  1807.     }
  1808.       else
  1809.         rl_point -= count;
  1810.     }
  1811.   return 0;
  1812. }
  1813.  
  1814. /* Move to the beginning of the line. */
  1815. rl_beg_of_line (count, key)
  1816.      int count, key;
  1817. {
  1818.   rl_point = 0;
  1819.   return 0;
  1820. }
  1821.  
  1822. /* Move to the end of the line. */
  1823. rl_end_of_line (count, key)
  1824.      int count, key;
  1825. {
  1826.   rl_point = rl_end;
  1827.   return 0;
  1828. }
  1829.  
  1830. /* Move forward a word.  We do what Emacs does. */
  1831. rl_forward_word (count, key)
  1832.      int count, key;
  1833. {
  1834.   int c;
  1835.  
  1836.   if (count < 0)
  1837.     {
  1838.       rl_backward_word (-count);
  1839.       return 0;
  1840.     }
  1841.  
  1842.   while (count)
  1843.     {
  1844.       if (rl_point == rl_end)
  1845.     return 0;
  1846.  
  1847.       /* If we are not in a word, move forward until we are in one.
  1848.      Then, move forward until we hit a non-alphabetic character. */
  1849.       c = the_line[rl_point];
  1850.       if (!alphabetic (c))
  1851.     {
  1852.       while (++rl_point < rl_end)
  1853.         {
  1854.           c = the_line[rl_point];
  1855.           if (alphabetic (c))
  1856.         break;
  1857.         }
  1858.     }
  1859.       if (rl_point == rl_end)
  1860.     return 0;
  1861.       while (++rl_point < rl_end)
  1862.     {
  1863.       c = the_line[rl_point];
  1864.       if (!alphabetic (c))
  1865.         break;
  1866.     }
  1867.       --count;
  1868.     }
  1869.   return 0;
  1870. }
  1871.  
  1872. /* Move backward a word.  We do what Emacs does. */
  1873. rl_backward_word (count, key)
  1874.      int count, key;
  1875. {
  1876.   int c;
  1877.  
  1878.   if (count < 0)
  1879.     {
  1880.       rl_forward_word (-count);
  1881.       return 0;
  1882.     }
  1883.  
  1884.   while (count)
  1885.     {
  1886.       if (!rl_point)
  1887.     return 0;
  1888.  
  1889.       /* Like rl_forward_word (), except that we look at the characters
  1890.      just before point. */
  1891.  
  1892.       c = the_line[rl_point - 1];
  1893.       if (!alphabetic (c))
  1894.     {
  1895.       while (--rl_point)
  1896.         {
  1897.           c = the_line[rl_point - 1];
  1898.           if (alphabetic (c))
  1899.         break;
  1900.         }
  1901.     }
  1902.  
  1903.       while (rl_point)
  1904.     {
  1905.       c = the_line[rl_point - 1];
  1906.       if (!alphabetic (c))
  1907.         break;
  1908.       else
  1909.         --rl_point;
  1910.     }
  1911.       --count;
  1912.     }
  1913.   return 0;
  1914. }
  1915.  
  1916. /* Clear the current line.  Numeric argument to C-l does this. */
  1917. rl_refresh_line ()
  1918. {
  1919.   int curr_line, nleft;
  1920.  
  1921.   /* Find out whether or not there might be invisible characters in the
  1922.      editing buffer. */
  1923.   if (rl_display_prompt == rl_prompt)
  1924.     nleft = _rl_last_c_pos - screenwidth - rl_visible_prompt_length;
  1925.   else
  1926.     nleft = _rl_last_c_pos - screenwidth;
  1927.  
  1928.   if (nleft > 0)
  1929.     curr_line = 1 + nleft / screenwidth;
  1930.   else
  1931.     curr_line = 0;
  1932.  
  1933.   _rl_move_vert (curr_line);
  1934.   _rl_move_cursor_relative (0, the_line);   /* XXX is this right */
  1935.  
  1936. #if defined (__GO32__)
  1937.   {
  1938.     int row, col, width, row_start;
  1939.  
  1940.     ScreenGetCursor (&row, &col);
  1941.     width = ScreenCols ();
  1942.     row_start = ScreenPrimary + (row * width);
  1943.     memset (row_start + col, 0, (width - col) * 2);
  1944.   }
  1945. #else /* !__GO32__ */
  1946.   if (term_clreol)
  1947.     tputs (term_clreol, 1, _rl_output_character_function);
  1948. #endif /* !__GO32__ */
  1949.  
  1950.   rl_forced_update_display ();
  1951.   rl_display_fixed = 1;
  1952.  
  1953.   return 0;
  1954. }
  1955.  
  1956. /* C-l typed to a line without quoting clears the screen, and then reprints
  1957.    the prompt and the current input line.  Given a numeric arg, redraw only
  1958.    the current line. */
  1959. rl_clear_screen (count, key)
  1960.      int count, key;
  1961. {
  1962.   if (rl_explicit_arg)
  1963.     {
  1964.       rl_refresh_line ();
  1965.       return 0;
  1966.     }
  1967.  
  1968. #if !defined (__GO32__)
  1969.   if (term_clrpag)
  1970.     tputs (term_clrpag, 1, _rl_output_character_function);
  1971.   else
  1972. #endif /* !__GO32__ */
  1973.     crlf ();
  1974.  
  1975.   rl_forced_update_display ();
  1976.   rl_display_fixed = 1;
  1977.  
  1978.   return 0;
  1979. }
  1980.  
  1981. rl_arrow_keys (count, c)
  1982.      int count, c;
  1983. {
  1984.   int ch;
  1985.  
  1986.   ch = rl_read_key ();
  1987.  
  1988.   switch (to_upper (ch))
  1989.     {
  1990.     case 'A':
  1991.       rl_get_previous_history (count);
  1992.       break;
  1993.  
  1994.     case 'B':
  1995.       rl_get_next_history (count);
  1996.       break;
  1997.  
  1998.     case 'C':
  1999.       rl_forward (count);
  2000.       break;
  2001.  
  2002.     case 'D':
  2003.       rl_backward (count);
  2004.       break;
  2005.  
  2006.     default:
  2007.       ding ();
  2008.     }
  2009.   return 0;
  2010. }
  2011.  
  2012.  
  2013. /* **************************************************************** */
  2014. /*                                    */
  2015. /*            Text commands                    */
  2016. /*                                    */
  2017. /* **************************************************************** */
  2018.  
  2019. /* Insert the character C at the current location, moving point forward. */
  2020. rl_insert (count, c)
  2021.      int count, c;
  2022. {
  2023.   register int i;
  2024.   char *string;
  2025.  
  2026.   if (count <= 0)
  2027.     return 0;
  2028.  
  2029.   /* If we can optimize, then do it.  But don't let people crash
  2030.      readline because of extra large arguments. */
  2031.   if (count > 1 && count < 1024)
  2032.     {
  2033.       string = xmalloc (1 + count);
  2034.  
  2035.       for (i = 0; i < count; i++)
  2036.     string[i] = c;
  2037.  
  2038.       string[i] = '\0';
  2039.       rl_insert_text (string);
  2040.       free (string);
  2041.  
  2042.       return 0;
  2043.     }
  2044.  
  2045.   if (count > 1024)
  2046.     {
  2047.       int decreaser;
  2048.       char str[1024+1];
  2049.  
  2050.       for (i = 0; i < 1024; i++)
  2051.     str[i] = c;
  2052.  
  2053.       while (count)
  2054.     {
  2055.       decreaser = (count > 1024 ? 1024 : count);
  2056.       str[decreaser] = '\0';
  2057.       rl_insert_text (str);
  2058.       count -= decreaser;
  2059.     }
  2060.  
  2061.       return 0;
  2062.     }
  2063.  
  2064.   /* We are inserting a single character.
  2065.      If there is pending input, then make a string of all of the
  2066.      pending characters that are bound to rl_insert, and insert
  2067.      them all. */
  2068.   if (any_typein)
  2069.     {
  2070.       int key = 0, t;
  2071.  
  2072.       i = 0;
  2073.       string = xmalloc (ibuffer_len + 1);
  2074.       string[i++] = c;
  2075.  
  2076.       while ((t = rl_get_char (&key)) &&
  2077.          (_rl_keymap[key].type == ISFUNC &&
  2078.           _rl_keymap[key].function == rl_insert))
  2079.     string[i++] = key;
  2080.  
  2081.       if (t)
  2082.     rl_unget_char (key);
  2083.  
  2084.       string[i] = '\0';
  2085.       rl_insert_text (string);
  2086.       free (string);
  2087.     }
  2088.   else
  2089.     {
  2090.       /* Inserting a single character. */
  2091.       char str[2];
  2092.  
  2093.       str[1] = '\0';
  2094.       str[0] = c;
  2095.       rl_insert_text (str);
  2096.     }
  2097.   return 0;
  2098. }
  2099.  
  2100. /* Insert the next typed character verbatim. */
  2101. rl_quoted_insert (count, key)
  2102.      int count, key;
  2103. {
  2104.   int c;
  2105.  
  2106.   c = rl_read_key ();
  2107.   return (rl_insert (count, c));  
  2108. }
  2109.  
  2110. /* Insert a tab character. */
  2111. rl_tab_insert (count, key)
  2112.      int count, key;
  2113. {
  2114.   return (rl_insert (count, '\t'));
  2115. }
  2116.  
  2117. /* What to do when a NEWLINE is pressed.  We accept the whole line.
  2118.    KEY is the key that invoked this command.  I guess it could have
  2119.    meaning in the future. */
  2120. rl_newline (count, key)
  2121.      int count, key;
  2122. {
  2123.   rl_done = 1;
  2124.  
  2125. #if defined (VI_MODE)
  2126.   _rl_vi_done_inserting ();
  2127.   _rl_vi_reset_last ();
  2128.  
  2129. #endif /* VI_MODE */
  2130.  
  2131.   if (readline_echoing_p)
  2132.     _rl_update_final ();
  2133.   return 0;
  2134. }
  2135.  
  2136. rl_clean_up_for_exit ()
  2137. {
  2138.   if (readline_echoing_p)
  2139.     {
  2140.       _rl_move_vert (_rl_vis_botlin);
  2141.       _rl_vis_botlin = 0;
  2142.       fflush (out_stream);
  2143.       rl_restart_output ();
  2144.     }
  2145.   return 0;
  2146. }
  2147.  
  2148. /* What to do for some uppercase characters, like meta characters,
  2149.    and some characters appearing in emacs_ctlx_keymap.  This function
  2150.    is just a stub, you bind keys to it and the code in _rl_dispatch ()
  2151.    is special cased. */
  2152. rl_do_lowercase_version (ignore1, ignore2)
  2153.      int ignore1, ignore2;
  2154. {
  2155.   return 0;
  2156. }
  2157.  
  2158. /* Rubout the character behind point. */
  2159. rl_rubout (count, key)
  2160.      int count, key;
  2161. {
  2162.   if (count < 0)
  2163.     {
  2164.       rl_delete (-count);
  2165.       return 0;
  2166.     }
  2167.  
  2168.   if (!rl_point)
  2169.     {
  2170.       ding ();
  2171.       return -1;
  2172.     }
  2173.  
  2174.   if (count > 1 || rl_explicit_arg)
  2175.     {
  2176.       int orig_point = rl_point;
  2177.       rl_backward (count);
  2178.       rl_kill_text (orig_point, rl_point);
  2179.     }
  2180.   else
  2181.     {
  2182.       int c = the_line[--rl_point];
  2183.       rl_delete_text (rl_point, rl_point + 1);
  2184.  
  2185.       if (rl_point == rl_end && isprint (c) && _rl_last_c_pos)
  2186.     {
  2187.       int l;
  2188.       l = rl_character_len (c, rl_point);
  2189.       _rl_erase_at_end_of_line (l);
  2190.     }
  2191.     }
  2192.   return 0;
  2193. }
  2194.  
  2195. /* Delete the character under the cursor.  Given a numeric argument,
  2196.    kill that many characters instead. */
  2197. rl_delete (count, invoking_key)
  2198.      int count, invoking_key;
  2199. {
  2200.   if (count < 0)
  2201.     {
  2202.       return (rl_rubout (-count));
  2203.     }
  2204.  
  2205.   if (rl_point == rl_end)
  2206.     {
  2207.       ding ();
  2208.       return -1;
  2209.     }
  2210.  
  2211.   if (count > 1 || rl_explicit_arg)
  2212.     {
  2213.       int orig_point = rl_point;
  2214.       rl_forward (count);
  2215.       rl_kill_text (orig_point, rl_point);
  2216.       rl_point = orig_point;
  2217.       return 0;
  2218.     }
  2219.   else
  2220.     return (rl_delete_text (rl_point, rl_point + 1));
  2221.   
  2222. }
  2223.  
  2224. /* Delete all spaces and tabs around point. */
  2225. rl_delete_horizontal_space (count, ignore)
  2226.      int count, ignore;
  2227. {
  2228.   int start = rl_point;
  2229.  
  2230.   while (rl_point && whitespace (the_line[rl_point - 1]))
  2231.     rl_point--;
  2232.  
  2233.   start = rl_point;
  2234.  
  2235.   while (rl_point < rl_end && whitespace (the_line[rl_point]))
  2236.     rl_point++;
  2237.  
  2238.   if (start != rl_point)
  2239.     {
  2240.       rl_delete_text (start, rl_point);
  2241.       rl_point = start;
  2242.     }
  2243.   return 0;
  2244. }
  2245.  
  2246.  
  2247. /* **************************************************************** */
  2248. /*                                    */
  2249. /*            Kill commands                    */
  2250. /*                                    */
  2251. /* **************************************************************** */
  2252.  
  2253. /* The next two functions mimic unix line editing behaviour, except they
  2254.    save the deleted text on the kill ring.  This is safer than not saving
  2255.    it, and since we have a ring, nobody should get screwed. */
  2256.  
  2257. /* This does what C-w does in Unix.  We can't prevent people from
  2258.    using behaviour that they expect. */
  2259. rl_unix_word_rubout (count, key)
  2260.      int count, key;
  2261. {
  2262.   if (!rl_point)
  2263.     ding ();
  2264.   else
  2265.     {
  2266.       int orig_point = rl_point;
  2267.  
  2268.       while (rl_point && whitespace (the_line[rl_point - 1]))
  2269.     rl_point--;
  2270.  
  2271.       while (rl_point && !whitespace (the_line[rl_point - 1]))
  2272.     rl_point--;
  2273.  
  2274.       rl_kill_text (orig_point, rl_point);
  2275.     }
  2276.   return 0;
  2277. }
  2278.  
  2279. /* Here is C-u doing what Unix does.  You don't *have* to use these
  2280.    key-bindings.  We have a choice of killing the entire line, or
  2281.    killing from where we are to the start of the line.  We choose the
  2282.    latter, because if you are a Unix weenie, then you haven't backspaced
  2283.    into the line at all, and if you aren't, then you know what you are
  2284.    doing. */
  2285. rl_unix_line_discard (count, key)
  2286.      int count, key;
  2287. {
  2288.   if (!rl_point)
  2289.     ding ();
  2290.   else
  2291.     {
  2292.       rl_kill_text (rl_point, 0);
  2293.       rl_point = 0;
  2294.     }
  2295.   return 0;
  2296. }
  2297.  
  2298.  
  2299. /* **************************************************************** */
  2300. /*                                    */
  2301. /*            Commands For Typos                */
  2302. /*                                    */
  2303. /* **************************************************************** */
  2304.  
  2305. /* Random and interesting things in here.  */
  2306.  
  2307. /* **************************************************************** */
  2308. /*                                    */
  2309. /*            Changing Case                    */
  2310. /*                                    */
  2311. /* **************************************************************** */
  2312.  
  2313. /* The three kinds of things that we know how to do. */
  2314. #define UpCase 1
  2315. #define DownCase 2
  2316. #define CapCase 3
  2317.  
  2318. static int rl_change_case ();
  2319.  
  2320. /* Uppercase the word at point. */
  2321. rl_upcase_word (count, key)
  2322.      int count, key;
  2323. {
  2324.   return (rl_change_case (count, UpCase));
  2325. }
  2326.  
  2327. /* Lowercase the word at point. */
  2328. rl_downcase_word (count, key)
  2329.      int count, key;
  2330. {
  2331.   return (rl_change_case (count, DownCase));
  2332. }
  2333.  
  2334. /* Upcase the first letter, downcase the rest. */
  2335. rl_capitalize_word (count, key)
  2336.      int count, key;
  2337. {
  2338.  return (rl_change_case (count, CapCase));
  2339. }
  2340.  
  2341. /* The meaty function.
  2342.    Change the case of COUNT words, performing OP on them.
  2343.    OP is one of UpCase, DownCase, or CapCase.
  2344.    If a negative argument is given, leave point where it started,
  2345.    otherwise, leave it where it moves to. */
  2346. static int
  2347. rl_change_case (count, op)
  2348.      int count, op;
  2349. {
  2350.   register int start = rl_point, end;
  2351.   int state = 0;
  2352.  
  2353.   rl_forward_word (count);
  2354.   end = rl_point;
  2355.  
  2356.   if (count < 0)
  2357.     {
  2358.       int temp = start;
  2359.       start = end;
  2360.       end = temp;
  2361.     }
  2362.  
  2363.   /* We are going to modify some text, so let's prepare to undo it. */
  2364.   rl_modifying (start, end);
  2365.  
  2366.   for (; start < end; start++)
  2367.     {
  2368.       switch (op)
  2369.     {
  2370.     case UpCase:
  2371.       the_line[start] = to_upper (the_line[start]);
  2372.       break;
  2373.  
  2374.     case DownCase:
  2375.       the_line[start] = to_lower (the_line[start]);
  2376.       break;
  2377.  
  2378.     case CapCase:
  2379.       if (state == 0)
  2380.         {
  2381.           the_line[start] = to_upper (the_line[start]);
  2382.           state = 1;
  2383.         }
  2384.       else
  2385.         {
  2386.           the_line[start] = to_lower (the_line[start]);
  2387.         }
  2388.       if (!pure_alphabetic (the_line[start]))
  2389.         state = 0;
  2390.       break;
  2391.  
  2392.     default:
  2393.       abort ();
  2394.       return -1;
  2395.     }
  2396.     }
  2397.   rl_point = end;
  2398.   return 0;
  2399. }
  2400.  
  2401. /* **************************************************************** */
  2402. /*                                    */
  2403. /*            Transposition                    */
  2404. /*                                    */
  2405. /* **************************************************************** */
  2406.  
  2407. /* Transpose the words at point. */
  2408. rl_transpose_words (count, key)
  2409.      int count, key;
  2410. {
  2411.   char *word1, *word2;
  2412.   int w1_beg, w1_end, w2_beg, w2_end;
  2413.   int orig_point = rl_point;
  2414.  
  2415.   if (!count)
  2416.     return 0;
  2417.  
  2418.   /* Find the two words. */
  2419.   rl_forward_word (count);
  2420.   w2_end = rl_point;
  2421.   rl_backward_word (1);
  2422.   w2_beg = rl_point;
  2423.   rl_backward_word (count);
  2424.   w1_beg = rl_point;
  2425.   rl_forward_word (1);
  2426.   w1_end = rl_point;
  2427.  
  2428.   /* Do some check to make sure that there really are two words. */
  2429.   if ((w1_beg == w2_beg) || (w2_beg < w1_end))
  2430.     {
  2431.       ding ();
  2432.       rl_point = orig_point;
  2433.       return -1;
  2434.     }
  2435.  
  2436.   /* Get the text of the words. */
  2437.   word1 = rl_copy_text (w1_beg, w1_end);
  2438.   word2 = rl_copy_text (w2_beg, w2_end);
  2439.  
  2440.   /* We are about to do many insertions and deletions.  Remember them
  2441.      as one operation. */
  2442.   rl_begin_undo_group ();
  2443.  
  2444.   /* Do the stuff at word2 first, so that we don't have to worry
  2445.      about word1 moving. */
  2446.   rl_point = w2_beg;
  2447.   rl_delete_text (w2_beg, w2_end);
  2448.   rl_insert_text (word1);
  2449.  
  2450.   rl_point = w1_beg;
  2451.   rl_delete_text (w1_beg, w1_end);
  2452.   rl_insert_text (word2);
  2453.  
  2454.   /* This is exactly correct since the text before this point has not
  2455.      changed in length. */
  2456.   rl_point = w2_end;
  2457.  
  2458.   /* I think that does it. */
  2459.   rl_end_undo_group ();
  2460.   free (word1);
  2461.   free (word2);
  2462.  
  2463.   return 0;
  2464. }
  2465.  
  2466. /* Transpose the characters at point.  If point is at the end of the line,
  2467.    then transpose the characters before point. */
  2468. rl_transpose_chars (count, key)
  2469.      int count, key;
  2470. {
  2471.   char dummy[2];
  2472.  
  2473.   if (!count)
  2474.     return 0;
  2475.  
  2476.   if (!rl_point || rl_end < 2)
  2477.     {
  2478.       ding ();
  2479.       return -1;
  2480.     }
  2481.  
  2482.   rl_begin_undo_group ();
  2483.  
  2484.   if (rl_point == rl_end)
  2485.     {
  2486.       --rl_point;
  2487.       count = 1;
  2488.     }
  2489.   rl_point--;
  2490.  
  2491.   dummy[0] = the_line[rl_point];
  2492.   dummy[1] = '\0';
  2493.  
  2494.   rl_delete_text (rl_point, rl_point + 1);
  2495.  
  2496.   rl_point += count;
  2497.   if (rl_point > rl_end)
  2498.     rl_point = rl_end;
  2499.   else if (rl_point < 0)
  2500.     rl_point = 0;
  2501.   rl_insert_text (dummy);
  2502.  
  2503.   rl_end_undo_group ();
  2504.   return 0;
  2505. }
  2506.  
  2507. /* **************************************************************** */
  2508. /*                                    */
  2509. /*            Undo, and Undoing                */
  2510. /*                                    */
  2511. /* **************************************************************** */
  2512.  
  2513. /* The current undo list for THE_LINE. */
  2514. UNDO_LIST *rl_undo_list = (UNDO_LIST *)NULL;
  2515.  
  2516. /* Remember how to undo something.  Concatenate some undos if that
  2517.    seems right. */
  2518. void
  2519. rl_add_undo (what, start, end, text)
  2520.      enum undo_code what;
  2521.      int start, end;
  2522.      char *text;
  2523. {
  2524.   UNDO_LIST *temp = (UNDO_LIST *)xmalloc (sizeof (UNDO_LIST));
  2525.   temp->what = what;
  2526.   temp->start = start;
  2527.   temp->end = end;
  2528.   temp->text = text;
  2529.   temp->next = rl_undo_list;
  2530.   rl_undo_list = temp;
  2531. }
  2532.  
  2533. /* Free the existing undo list. */
  2534. void
  2535. free_undo_list ()
  2536. {
  2537.   while (rl_undo_list)
  2538.     {
  2539.       UNDO_LIST *release = rl_undo_list;
  2540.       rl_undo_list = rl_undo_list->next;
  2541.  
  2542.       if (release->what == UNDO_DELETE)
  2543.     free (release->text);
  2544.  
  2545.       free (release);
  2546.     }
  2547.   rl_undo_list = (UNDO_LIST *)NULL;
  2548. }
  2549.  
  2550. /* Undo the next thing in the list.  Return 0 if there
  2551.    is nothing to undo, or non-zero if there was. */
  2552. int
  2553. rl_do_undo ()
  2554. {
  2555.   UNDO_LIST *release;
  2556.   int waiting_for_begin = 0;
  2557.  
  2558. undo_thing:
  2559.   if (!rl_undo_list)
  2560.     return (0);
  2561.  
  2562.   doing_an_undo = 1;
  2563.  
  2564.   switch (rl_undo_list->what) {
  2565.  
  2566.     /* Undoing deletes means inserting some text. */
  2567.   case UNDO_DELETE:
  2568.     rl_point = rl_undo_list->start;
  2569.     rl_insert_text (rl_undo_list->text);
  2570.     free (rl_undo_list->text);
  2571.     break;
  2572.  
  2573.     /* Undoing inserts means deleting some text. */
  2574.   case UNDO_INSERT:
  2575.     rl_delete_text (rl_undo_list->start, rl_undo_list->end);
  2576.     rl_point = rl_undo_list->start;
  2577.     break;
  2578.  
  2579.     /* Undoing an END means undoing everything 'til we get to
  2580.        a BEGIN. */
  2581.   case UNDO_END:
  2582.     waiting_for_begin++;
  2583.     break;
  2584.  
  2585.     /* Undoing a BEGIN means that we are done with this group. */
  2586.   case UNDO_BEGIN:
  2587.     if (waiting_for_begin)
  2588.       waiting_for_begin--;
  2589.     else
  2590.       ding ();
  2591.     break;
  2592.   }
  2593.  
  2594.   doing_an_undo = 0;
  2595.  
  2596.   release = rl_undo_list;
  2597.   rl_undo_list = rl_undo_list->next;
  2598.   free (release);
  2599.  
  2600.   if (waiting_for_begin)
  2601.     goto undo_thing;
  2602.  
  2603.   return (1);
  2604. }
  2605.  
  2606. /* Begin a group.  Subsequent undos are undone as an atomic operation. */
  2607. int
  2608. rl_begin_undo_group ()
  2609. {
  2610.   rl_add_undo (UNDO_BEGIN, 0, 0, 0);
  2611.   return 0;
  2612. }
  2613.  
  2614. /* End an undo group started with rl_begin_undo_group (). */
  2615. int
  2616. rl_end_undo_group ()
  2617. {
  2618.   rl_add_undo (UNDO_END, 0, 0, 0);
  2619.   return 0;
  2620. }
  2621.  
  2622. /* Save an undo entry for the text from START to END. */
  2623. rl_modifying (start, end)
  2624.      int start, end;
  2625. {
  2626.   if (start > end)
  2627.     {
  2628.       int t = start;
  2629.       start = end;
  2630.       end = t;
  2631.     }
  2632.  
  2633.   if (start != end)
  2634.     {
  2635.       char *temp = rl_copy_text (start, end);
  2636.       rl_begin_undo_group ();
  2637.       rl_add_undo (UNDO_DELETE, start, end, temp);
  2638.       rl_add_undo (UNDO_INSERT, start, end, (char *)NULL);
  2639.       rl_end_undo_group ();
  2640.     }
  2641.   return 0;
  2642. }
  2643.  
  2644. /* Revert the current line to its previous state. */
  2645. int
  2646. rl_revert_line (count, key)
  2647.      int count, key;
  2648. {
  2649.   if (!rl_undo_list)
  2650.     ding ();
  2651.   else
  2652.     {
  2653.       while (rl_undo_list)
  2654.     rl_do_undo ();
  2655.     }
  2656.   return 0;
  2657. }
  2658.  
  2659. /* Do some undoing of things that were done. */
  2660. int
  2661. rl_undo_command (count, key)
  2662.      int count, key;
  2663. {
  2664.   if (count < 0)
  2665.     return 0;    /* Nothing to do. */
  2666.  
  2667.   while (count)
  2668.     {
  2669.       if (rl_do_undo ())
  2670.     count--;
  2671.       else
  2672.     {
  2673.       ding ();
  2674.       break;
  2675.     }
  2676.     }
  2677.   return 0;
  2678. }
  2679.  
  2680. /* **************************************************************** */
  2681. /*                                    */
  2682. /*            History Utilities                */
  2683. /*                                    */
  2684. /* **************************************************************** */
  2685.  
  2686. /* We already have a history library, and that is what we use to control
  2687.    the history features of readline.  However, this is our local interface
  2688.    to the history mechanism. */
  2689.  
  2690. /* While we are editing the history, this is the saved
  2691.    version of the original line. */
  2692. HIST_ENTRY *saved_line_for_history = (HIST_ENTRY *)NULL;
  2693.  
  2694. /* Set the history pointer back to the last entry in the history. */
  2695. static void
  2696. start_using_history ()
  2697. {
  2698.   using_history ();
  2699.   if (saved_line_for_history)
  2700.     _rl_free_history_entry (saved_line_for_history);
  2701.  
  2702.   saved_line_for_history = (HIST_ENTRY *)NULL;
  2703. }
  2704.  
  2705. /* Free the contents (and containing structure) of a HIST_ENTRY. */
  2706. void
  2707. _rl_free_history_entry (entry)
  2708.      HIST_ENTRY *entry;
  2709. {
  2710.   if (!entry)
  2711.     return;
  2712.   if (entry->line)
  2713.     free (entry->line);
  2714.   free (entry);
  2715. }
  2716.  
  2717. /* Perhaps put back the current line if it has changed. */
  2718. maybe_replace_line ()
  2719. {
  2720.   HIST_ENTRY *temp = current_history ();
  2721.  
  2722.   /* If the current line has changed, save the changes. */
  2723.   if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list))
  2724.     {
  2725.       temp = replace_history_entry (where_history (), the_line, rl_undo_list);
  2726.       free (temp->line);
  2727.       free (temp);
  2728.     }
  2729.   return 0;
  2730. }
  2731.  
  2732. /* Put back the saved_line_for_history if there is one. */
  2733. maybe_unsave_line ()
  2734. {
  2735.   if (saved_line_for_history)
  2736.     {
  2737.       int line_len;
  2738.  
  2739.       line_len = strlen (saved_line_for_history->line);
  2740.  
  2741.       if (line_len >= rl_line_buffer_len)
  2742.     rl_extend_line_buffer (line_len);
  2743.  
  2744.       strcpy (the_line, saved_line_for_history->line);
  2745.       rl_undo_list = (UNDO_LIST *)saved_line_for_history->data;
  2746.       _rl_free_history_entry (saved_line_for_history);
  2747.       saved_line_for_history = (HIST_ENTRY *)NULL;
  2748.       rl_end = rl_point = strlen (the_line);
  2749.     }
  2750.   else
  2751.     ding ();
  2752.   return 0;
  2753. }
  2754.  
  2755. /* Save the current line in saved_line_for_history. */
  2756. maybe_save_line ()
  2757. {
  2758.   if (!saved_line_for_history)
  2759.     {
  2760.       saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
  2761.       saved_line_for_history->line = savestring (the_line);
  2762.       saved_line_for_history->data = (char *)rl_undo_list;
  2763.     }
  2764.   return 0;
  2765. }
  2766.  
  2767. /* **************************************************************** */
  2768. /*                                    */
  2769. /*            History Commands                */
  2770. /*                                    */
  2771. /* **************************************************************** */
  2772.  
  2773. /* Meta-< goes to the start of the history. */
  2774. rl_beginning_of_history (count, key)
  2775.      int count, key;
  2776. {
  2777.   return (rl_get_previous_history (1 + where_history ()));
  2778. }
  2779.  
  2780. /* Meta-> goes to the end of the history.  (The current line). */
  2781. rl_end_of_history (count, key)
  2782.      int count, key;
  2783. {
  2784.   maybe_replace_line ();
  2785.   using_history ();
  2786.   maybe_unsave_line ();
  2787.   return 0;
  2788. }
  2789.  
  2790. /* Move down to the next history line. */
  2791. rl_get_next_history (count, key)
  2792.      int count, key;
  2793. {
  2794.   HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
  2795.  
  2796.   if (count < 0)
  2797.     return (rl_get_previous_history (-count));
  2798.  
  2799.   if (!count)
  2800.     return 0;
  2801.  
  2802.   maybe_replace_line ();
  2803.  
  2804.   while (count)
  2805.     {
  2806.       temp = next_history ();
  2807.       if (!temp)
  2808.     break;
  2809.       --count;
  2810.     }
  2811.  
  2812.   if (!temp)
  2813.     maybe_unsave_line ();
  2814.   else
  2815.     {
  2816.       int line_len;
  2817.  
  2818.       line_len = strlen (temp->line);
  2819.  
  2820.       if (line_len >= rl_line_buffer_len)
  2821.     rl_extend_line_buffer (line_len);
  2822.  
  2823.       strcpy (the_line, temp->line);
  2824.       rl_undo_list = (UNDO_LIST *)temp->data;
  2825.       rl_end = rl_point = strlen (the_line);
  2826. #if defined (VI_MODE)
  2827.       if (rl_editing_mode == vi_mode)
  2828.     rl_point = 0;
  2829. #endif /* VI_MODE */
  2830.     }
  2831.   return 0;
  2832. }
  2833.  
  2834. /* Get the previous item out of our interactive history, making it the current
  2835.    line.  If there is no previous history, just ding. */
  2836. rl_get_previous_history (count, key)
  2837.      int count, key;
  2838. {
  2839.   HIST_ENTRY *old_temp = (HIST_ENTRY *)NULL;
  2840.   HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
  2841.  
  2842.   if (count < 0)
  2843.     return (rl_get_next_history (-count));
  2844.  
  2845.   if (!count)
  2846.     return 0;
  2847.  
  2848.   /* If we don't have a line saved, then save this one. */
  2849.   maybe_save_line ();
  2850.  
  2851.   /* If the current line has changed, save the changes. */
  2852.   maybe_replace_line ();
  2853.  
  2854.   while (count)
  2855.     {
  2856.       temp = previous_history ();
  2857.       if (!temp)
  2858.     break;
  2859.       else
  2860.     old_temp = temp;
  2861.       --count;
  2862.     }
  2863.  
  2864.   /* If there was a large argument, and we moved back to the start of the
  2865.      history, that is not an error.  So use the last value found. */
  2866.   if (!temp && old_temp)
  2867.     temp = old_temp;
  2868.  
  2869.   if (!temp)
  2870.     ding ();
  2871.   else
  2872.     {
  2873.       int line_len;
  2874.  
  2875.       line_len = strlen (temp->line);
  2876.  
  2877.       if (line_len >= rl_line_buffer_len)
  2878.     rl_extend_line_buffer (line_len);
  2879.  
  2880.       strcpy (the_line, temp->line);
  2881.       rl_undo_list = (UNDO_LIST *)temp->data;
  2882.       rl_end = rl_point = line_len;
  2883.  
  2884. #if defined (VI_MODE)
  2885.       if (rl_editing_mode == vi_mode)
  2886.     rl_point = 0;
  2887. #endif /* VI_MODE */
  2888.     }
  2889.   return 0;
  2890. }
  2891.  
  2892. /* Make C be the next command to be executed. */
  2893. rl_execute_next (c)
  2894.      int c;
  2895. {
  2896.   rl_pending_input = c;
  2897.   return 0;
  2898. }
  2899.  
  2900. /* **************************************************************** */
  2901. /*                                    */
  2902. /*           The Mark and the Region.                */
  2903. /*                                    */
  2904. /* **************************************************************** */
  2905.  
  2906. /* Set the mark at POSITION. */
  2907. rl_set_mark (position)
  2908.      int position;
  2909. {
  2910.   if (position > rl_end)
  2911.     return -1;
  2912.  
  2913.   rl_mark = position;
  2914.   return 0;
  2915. }
  2916.  
  2917. /* Exchange the position of mark and point. */
  2918. rl_exchange_mark_and_point (count, key)
  2919.      int count, key;
  2920. {
  2921.   if (rl_mark > rl_end)
  2922.     rl_mark = -1;
  2923.  
  2924.   if (rl_mark == -1)
  2925.     {
  2926.       ding ();
  2927.       return -1;
  2928.     }
  2929.   else
  2930.     {
  2931.       int temp = rl_point;
  2932.  
  2933.       rl_point = rl_mark;
  2934.       rl_mark = temp;
  2935.     }
  2936.   return 0;
  2937. }
  2938.  
  2939.  
  2940. /* **************************************************************** */
  2941. /*                                    */
  2942. /*            Killing Mechanism                */
  2943. /*                                    */
  2944. /* **************************************************************** */
  2945.  
  2946. /* What we assume for a max number of kills. */
  2947. #define DEFAULT_MAX_KILLS 10
  2948.  
  2949. /* The real variable to look at to find out when to flush kills. */
  2950. int rl_max_kills =  DEFAULT_MAX_KILLS;
  2951.  
  2952. /* Where to store killed text. */
  2953. char **rl_kill_ring = (char **)NULL;
  2954.  
  2955. /* Where we are in the kill ring. */
  2956. int rl_kill_index = 0;
  2957.  
  2958. /* How many slots we have in the kill ring. */
  2959. int rl_kill_ring_length = 0;
  2960.  
  2961. /* How to say that you only want to save a certain amount
  2962.    of kill material. */
  2963. rl_set_retained_kills (num)
  2964.      int num;
  2965. {
  2966.   return 0;
  2967. }
  2968.  
  2969. /* The way to kill something.  This appends or prepends to the last
  2970.    kill, if the last command was a kill command.  if FROM is less
  2971.    than TO, then the text is appended, otherwise prepended.  If the
  2972.    last command was not a kill command, then a new slot is made for
  2973.    this kill. */
  2974. rl_kill_text (from, to)
  2975.      int from, to;
  2976. {
  2977.   int slot;
  2978.   char *text;
  2979.  
  2980.   /* Is there anything to kill? */
  2981.   if (from == to)
  2982.     {
  2983.       last_command_was_kill++;
  2984.       return 0;
  2985.     }
  2986.  
  2987.   text = rl_copy_text (from, to);
  2988.  
  2989.   /* Delete the copied text from the line. */
  2990.   rl_delete_text (from, to);
  2991.  
  2992.   /* First, find the slot to work with. */
  2993.   if (!last_command_was_kill)
  2994.     {
  2995.       /* Get a new slot.  */
  2996.       if (!rl_kill_ring)
  2997.     {
  2998.       /* If we don't have any defined, then make one. */
  2999.       rl_kill_ring = (char **)
  3000.         xmalloc (((rl_kill_ring_length = 1) + 1) * sizeof (char *));
  3001.       rl_kill_ring[slot = 0] = (char *)NULL;
  3002.     }
  3003.       else
  3004.     {
  3005.       /* We have to add a new slot on the end, unless we have
  3006.          exceeded the max limit for remembering kills. */
  3007.       slot = rl_kill_ring_length;
  3008.       if (slot == rl_max_kills)
  3009.         {
  3010.           register int i;
  3011.           free (rl_kill_ring[0]);
  3012.           for (i = 0; i < slot; i++)
  3013.         rl_kill_ring[i] = rl_kill_ring[i + 1];
  3014.         }
  3015.       else
  3016.         {
  3017.           slot = rl_kill_ring_length += 1;
  3018.           rl_kill_ring = (char **)xrealloc (rl_kill_ring, slot * sizeof (char *));
  3019.         }
  3020.       rl_kill_ring[--slot] = (char *)NULL;
  3021.     }
  3022.     }
  3023.   else
  3024.     slot = rl_kill_ring_length - 1;
  3025.  
  3026.   /* If the last command was a kill, prepend or append. */
  3027.   if (last_command_was_kill && rl_editing_mode != vi_mode)
  3028.     {
  3029.       char *old = rl_kill_ring[slot];
  3030.       char *new = xmalloc (1 + strlen (old) + strlen (text));
  3031.  
  3032.       if (from < to)
  3033.     {
  3034.       strcpy (new, old);
  3035.       strcat (new, text);
  3036.     }
  3037.       else
  3038.     {
  3039.       strcpy (new, text);
  3040.       strcat (new, old);
  3041.     }
  3042.       free (old);
  3043.       free (text);
  3044.       rl_kill_ring[slot] = new;
  3045.     }
  3046.   else
  3047.     {
  3048.       rl_kill_ring[slot] = text;
  3049.     }
  3050.   rl_kill_index = slot;
  3051.   last_command_was_kill++;
  3052.   return 0;
  3053. }
  3054.  
  3055. /* Now REMEMBER!  In order to do prepending or appending correctly, kill
  3056.    commands always make rl_point's original position be the FROM argument,
  3057.    and rl_point's extent be the TO argument. */
  3058.  
  3059. /* **************************************************************** */
  3060. /*                                    */
  3061. /*            Killing Commands                */
  3062. /*                                    */
  3063. /* **************************************************************** */
  3064.  
  3065. /* Delete the word at point, saving the text in the kill ring. */
  3066. rl_kill_word (count, key)
  3067.      int count, key;
  3068. {
  3069.   int orig_point = rl_point;
  3070.  
  3071.   if (count < 0)
  3072.     return (rl_backward_kill_word (-count));
  3073.   else
  3074.     {
  3075.       rl_forward_word (count);
  3076.  
  3077.       if (rl_point != orig_point)
  3078.     rl_kill_text (orig_point, rl_point);
  3079.  
  3080.       rl_point = orig_point;
  3081.     }
  3082.   return 0;
  3083. }
  3084.  
  3085. /* Rubout the word before point, placing it on the kill ring. */
  3086. rl_backward_kill_word (count, ignore)
  3087.      int count, ignore;
  3088. {
  3089.   int orig_point = rl_point;
  3090.  
  3091.   if (count < 0)
  3092.     return (rl_kill_word (-count));
  3093.   else
  3094.     {
  3095.       rl_backward_word (count);
  3096.  
  3097.       if (rl_point != orig_point)
  3098.     rl_kill_text (orig_point, rl_point);
  3099.     }
  3100.   return 0;
  3101. }
  3102.  
  3103. /* Kill from here to the end of the line.  If DIRECTION is negative, kill
  3104.    back to the line start instead. */
  3105. rl_kill_line (direction, ignore)
  3106.      int direction, ignore;
  3107. {
  3108.   int orig_point = rl_point;
  3109.  
  3110.   if (direction < 0)
  3111.     return (rl_backward_kill_line (1));
  3112.   else
  3113.     {
  3114.       rl_end_of_line ();
  3115.       if (orig_point != rl_point)
  3116.     rl_kill_text (orig_point, rl_point);
  3117.       rl_point = orig_point;
  3118.     }
  3119.   return 0;
  3120. }
  3121.  
  3122. /* Kill backwards to the start of the line.  If DIRECTION is negative, kill
  3123.    forwards to the line end instead. */
  3124. rl_backward_kill_line (direction, ignore)
  3125.      int direction, ignore;
  3126. {
  3127.   int orig_point = rl_point;
  3128.  
  3129.   if (direction < 0)
  3130.     return (rl_kill_line (1));
  3131.   else
  3132.     {
  3133.       if (!rl_point)
  3134.     ding ();
  3135.       else
  3136.     {
  3137.       rl_beg_of_line ();
  3138.       rl_kill_text (orig_point, rl_point);
  3139.     }
  3140.     }
  3141.   return 0;
  3142. }
  3143.  
  3144. /* Kill the whole line, no matter where point is. */
  3145. rl_kill_full_line (count, ignore)
  3146.      int count, ignore;
  3147. {
  3148.   rl_begin_undo_group ();
  3149.   rl_point = 0;
  3150.   rl_kill_text (rl_point, rl_end);
  3151.   rl_end_undo_group ();
  3152.   return 0;
  3153. }
  3154.  
  3155. /* Yank back the last killed text.  This ignores arguments. */
  3156. rl_yank (count, ignore)
  3157.      int count, ignore;
  3158. {
  3159.   if (!rl_kill_ring)
  3160.     {
  3161.       rl_abort ();
  3162.       return -1;
  3163.     }
  3164.  
  3165.   rl_set_mark (rl_point);
  3166.   rl_insert_text (rl_kill_ring[rl_kill_index]);
  3167.   return 0;
  3168. }
  3169.  
  3170. /* If the last command was yank, or yank_pop, and the text just
  3171.    before point is identical to the current kill item, then
  3172.    delete that text from the line, rotate the index down, and
  3173.    yank back some other text. */
  3174. rl_yank_pop (count, key)
  3175.      int count, key;
  3176. {
  3177.   int l;
  3178.  
  3179.   if (((rl_last_func != rl_yank_pop) && (rl_last_func != rl_yank)) ||
  3180.       !rl_kill_ring)
  3181.     {
  3182.       rl_abort ();
  3183.       return -1;
  3184.     }
  3185.  
  3186.   l = strlen (rl_kill_ring[rl_kill_index]);
  3187.   if (((rl_point - l) >= 0) &&
  3188.       (strncmp (the_line + (rl_point - l),
  3189.         rl_kill_ring[rl_kill_index], l) == 0))
  3190.     {
  3191.       rl_delete_text ((rl_point - l), rl_point);
  3192.       rl_point -= l;
  3193.       rl_kill_index--;
  3194.       if (rl_kill_index < 0)
  3195.     rl_kill_index = rl_kill_ring_length - 1;
  3196.       rl_yank (1, 0);
  3197.       return 0;
  3198.     }
  3199.   else
  3200.     {
  3201.       rl_abort ();
  3202.       return -1;
  3203.     }
  3204. }
  3205.  
  3206. /* Yank the COUNTth argument from the previous history line. */
  3207. rl_yank_nth_arg (count, ignore)
  3208.      int count, ignore;
  3209. {
  3210.   register HIST_ENTRY *entry = previous_history ();
  3211.   char *arg;
  3212.  
  3213.   if (entry)
  3214.     next_history ();
  3215.   else
  3216.     {
  3217.       ding ();
  3218.       return -1;
  3219.     }
  3220.  
  3221.   arg = history_arg_extract (count, count, entry->line);
  3222.   if (!arg || !*arg)
  3223.     {
  3224.       ding ();
  3225.       return -1;
  3226.     }
  3227.  
  3228.   rl_begin_undo_group ();
  3229.  
  3230. #if defined (VI_MODE)
  3231.   /* Vi mode always inserts a space before yanking the argument, and it
  3232.      inserts it right *after* rl_point. */
  3233.   if (rl_editing_mode == vi_mode)
  3234.     {
  3235.       rl_vi_append_mode ();
  3236.       rl_insert_text (" ");
  3237.     }
  3238. #endif /* VI_MODE */
  3239.  
  3240.   rl_insert_text (arg);
  3241.   free (arg);
  3242.  
  3243.   rl_end_undo_group ();
  3244.   return 0;
  3245. }
  3246.  
  3247. /* Yank the last argument from the previous history line.  This `knows'
  3248.    how rl_yank_nth_arg treats a count of `$'.  With an argument, this
  3249.    behaves the same as rl_yank_nth_arg. */
  3250. int
  3251. rl_yank_last_arg (count, key)
  3252.      int count, key;
  3253. {
  3254.   if (rl_explicit_arg)
  3255.     return (rl_yank_nth_arg (count, key));
  3256.   else
  3257.     return (rl_yank_nth_arg ('$', key));
  3258. }
  3259.  
  3260. /* How to toggle back and forth between editing modes. */
  3261. rl_vi_editing_mode (count, key)
  3262.      int count, key;
  3263. {
  3264. #if defined (VI_MODE)
  3265.   rl_editing_mode = vi_mode;
  3266.   rl_vi_insertion_mode ();
  3267.   return 0;
  3268. #endif /* VI_MODE */
  3269. }
  3270.  
  3271. rl_emacs_editing_mode (count, key)
  3272.      int count, key;
  3273. {
  3274.   rl_editing_mode = emacs_mode;
  3275.   _rl_keymap = emacs_standard_keymap;
  3276.   return 0;
  3277. }
  3278.  
  3279.  
  3280. /* **************************************************************** */
  3281. /*                                    */
  3282. /*            USG (System V) Support                */
  3283. /*                                    */
  3284. /* **************************************************************** */
  3285.  
  3286. int
  3287. rl_getc (stream)
  3288.      FILE *stream;
  3289. {
  3290.   int result;
  3291.   unsigned char c;
  3292.  
  3293. #if defined (__GO32__)
  3294.   if (isatty (0))
  3295.     return (getkey () & 0x7F);
  3296. #endif /* __GO32__ */
  3297.  
  3298.   while (1)
  3299.     {
  3300.       result = read (fileno (stream), &c, sizeof (unsigned char));
  3301.  
  3302.       if (result == sizeof (unsigned char))
  3303.     return (c);
  3304.  
  3305.       /* If zero characters are returned, then the file that we are
  3306.      reading from is empty!  Return EOF in that case. */
  3307.       if (result == 0)
  3308.     return (EOF);
  3309.  
  3310. #if defined (EWOULDBLOCK)
  3311.       if (errno == EWOULDBLOCK)
  3312.     {
  3313.       int flags;
  3314.  
  3315.       if ((flags = fcntl (fileno (stream), F_GETFL, 0)) < 0)
  3316.         return (EOF);
  3317.       if (flags & O_NDELAY)
  3318.         {
  3319.           flags &= ~O_NDELAY;
  3320.           fcntl (fileno (stream), F_SETFL, flags);
  3321.           continue;
  3322.         }
  3323.       continue;
  3324.     }
  3325. #endif /* EWOULDBLOCK */
  3326.  
  3327. #if defined (_POSIX_VERSION) && defined (EAGAIN) && defined (O_NONBLOCK)
  3328.       if (errno == EAGAIN)
  3329.     {
  3330.       int flags;
  3331.  
  3332.       if ((flags = fcntl (fileno (stream), F_GETFL, 0)) < 0)
  3333.         return (EOF);
  3334.       if (flags & O_NONBLOCK)
  3335.         {
  3336.           flags &= ~O_NONBLOCK;
  3337.           fcntl (fileno (stream), F_SETFL, flags);
  3338.           continue;
  3339.         }
  3340.     }
  3341. #endif /* _POSIX_VERSION && EAGAIN && O_NONBLOCK */
  3342.  
  3343. #if !defined (__GO32__)
  3344.       /* If the error that we received was SIGINT, then try again,
  3345.      this is simply an interrupted system call to read ().
  3346.      Otherwise, some error ocurred, also signifying EOF. */
  3347.       if (errno != EINTR)
  3348.     return (EOF);
  3349. #endif /* !__GO32__ */
  3350.     }
  3351. }
  3352.  
  3353. #if !defined (SHELL)
  3354. #ifdef savestring
  3355. #undef savestring
  3356. #endif
  3357. /* Backwards compatibilty, now that savestring has been removed from
  3358.    all `public' readline header files. */
  3359. char *
  3360. savestring (s)
  3361.      char *s;
  3362. {
  3363.   return ((char *)strcpy (xmalloc (1 + (int)strlen (s)), (s)));
  3364. }
  3365. #endif
  3366.  
  3367. /* Function equivalents for the macros defined in chartypes.h. */
  3368. #undef uppercase_p
  3369. int
  3370. uppercase_p (c)
  3371.      int c;
  3372. {
  3373.   return (isupper (c));
  3374. }
  3375.  
  3376. #undef lowercase_p
  3377. int
  3378. lowercase_p (c)
  3379.      int c;
  3380. {
  3381.   return (islower (c));
  3382. }
  3383.  
  3384. #undef pure_alphabetic
  3385. int
  3386. pure_alphabetic (c)
  3387.      int c;
  3388. {
  3389.   return (isupper (c) || islower (c));
  3390. }
  3391.  
  3392. #undef digit_p
  3393. int
  3394. digit_p (c)
  3395.      int c;
  3396. {
  3397.   return (isdigit (c));
  3398. }
  3399.  
  3400. #undef to_lower
  3401. int
  3402. to_lower (c)
  3403.      int c;
  3404. {
  3405.   return (isupper (c) ? tolower (c) : c);
  3406. }
  3407.  
  3408. #undef to_upper
  3409. int
  3410. to_upper (c)
  3411.      int c;
  3412. {
  3413.   return (islower (c) ? toupper (c) : c);
  3414. }
  3415.  
  3416. #undef digit_value
  3417. int
  3418. digit_value (c)
  3419.      int c;
  3420. {
  3421.   return (isdigit (c) ? c - '0' : c);
  3422. }
  3423.  
  3424. #if defined (STATIC_MALLOC)
  3425.  
  3426. /* **************************************************************** */
  3427. /*                                    */
  3428. /*            xmalloc and xrealloc ()                     */
  3429. /*                                    */
  3430. /* **************************************************************** */
  3431.  
  3432. static void memory_error_and_abort ();
  3433.  
  3434. static char *
  3435. xmalloc (bytes)
  3436.      int bytes;
  3437. {
  3438.   char *temp = (char *)malloc (bytes);
  3439.  
  3440.   if (!temp)
  3441.     memory_error_and_abort ();
  3442.   return (temp);
  3443. }
  3444.  
  3445. static char *
  3446. xrealloc (pointer, bytes)
  3447.      char *pointer;
  3448.      int bytes;
  3449. {
  3450.   char *temp;
  3451.  
  3452.   if (!pointer)
  3453.     temp = (char *)malloc (bytes);
  3454.   else
  3455.     temp = (char *)realloc (pointer, bytes);
  3456.  
  3457.   if (!temp)
  3458.     memory_error_and_abort ();
  3459.  
  3460.   return (temp);
  3461. }
  3462.  
  3463. static void
  3464. memory_error_and_abort ()
  3465. {
  3466.   fprintf (stderr, "readline: Out of virtual memory!\n");
  3467.   abort ();
  3468. }
  3469. #endif /* STATIC_MALLOC */
  3470.  
  3471.  
  3472. /* **************************************************************** */
  3473. /*                                    */
  3474. /*            Testing Readline                */
  3475. /*                                    */
  3476. /* **************************************************************** */
  3477.  
  3478. #if defined (TEST)
  3479.  
  3480. main ()
  3481. {
  3482.   HIST_ENTRY **history_list ();
  3483.   char *temp = (char *)NULL;
  3484.   char *prompt = "readline% ";
  3485.   int done = 0;
  3486.  
  3487.   while (!done)
  3488.     {
  3489.       temp = readline (prompt);
  3490.  
  3491.       /* Test for EOF. */
  3492.       if (!temp)
  3493.     exit (1);
  3494.  
  3495.       /* If there is anything on the line, print it and remember it. */
  3496.       if (*temp)
  3497.     {
  3498.       fprintf (stderr, "%s\r\n", temp);
  3499.       add_history (temp);
  3500.     }
  3501.  
  3502.       /* Check for `command' that we handle. */
  3503.       if (strcmp (temp, "quit") == 0)
  3504.     done = 1;
  3505.  
  3506.       if (strcmp (temp, "list") == 0)
  3507.     {
  3508.       HIST_ENTRY **list = history_list ();
  3509.       register int i;
  3510.       if (list)
  3511.         {
  3512.           for (i = 0; list[i]; i++)
  3513.         {
  3514.           fprintf (stderr, "%d: %s\r\n", i, list[i]->line);
  3515.           free (list[i]->line);
  3516.         }
  3517.           free (list);
  3518.         }
  3519.     }
  3520.       free (temp);
  3521.     }
  3522. }
  3523.  
  3524. #endif /* TEST */
  3525.  
  3526.  
  3527. /*
  3528.  * Local variables:
  3529.  * compile-command: "gcc -g -traditional -I. -I.. -DTEST -o readline readline.c keymaps.o funmap.o history.o -ltermcap"
  3530.  * end:
  3531.  */
  3532.